系统.c#建议

本文关键字:建议 系统 | 更新日期: 2023-09-27 18:15:58

当我试图运行我的程序时,我得到一个异常。在我看来,它工作得很好,但我似乎遗漏了一些东西。

 public Coord LatLonToUTMXY(double lat, double lon)
    {
        Coord co = new Coord();
        co.zone = Math.Floor((lon + 180.0) / 6) + 1;
        //double zone = Math.Floor((lon + 180.0) / 6) + 1;
        MapLatLonToXY(DegToRad(lat), DegToRad(lon), UTMCentralMeridian(co.zone),       co.eastNorth);
        /* Adjust easting and northing for UTM system. */
        co.eastNorth[0] = co.eastNorth[0] * UTMScaleFactor + 500000.0;
        co.eastNorth[1] = co.eastNorth[1] * UTMScaleFactor;
        if (co.eastNorth[1] < 0.0)
            co.eastNorth[1] = co.eastNorth[1] + 10000000.0;

        return co;
    }

我在这里得到异常…

 public void MapLatLonToXY(double phi, double lambda, double lambda0, double [] xy)
    {
       //Some previous code
       //Exception for xy[0] ....!
        xy[0] = N * Math.Cos(phi) * l
            + (N / 6.0 * Math.Pow(Math.Cos(phi), 3.0) * l3coef * Math.Pow(l, 3.0))
            + (N / 120.0 * Math.Pow(Math.Cos(phi), 5.0) * l5coef * Math.Pow(l, 5.0))
            + (N / 5040.0 * Math.Pow(Math.Cos(phi), 7.0) * l7coef * Math.Pow(l, 7.0));
        /* Calculate northing (y) */
        xy[1] = ArcLengthOfMeridian(phi)
            + (t / 2.0 * N * Math.Pow(Math.Cos(phi), 2.0) * Math.Pow(l, 2.0))
            + (t / 24.0 * N * Math.Pow(Math.Cos(phi), 4.0) * l4coef * Math.Pow(l, 4.0))
            + (t / 720.0 * N * Math.Pow(Math.Cos(phi), 6.0) * l6coef * Math.Pow(l, 6.0))
            + (t / 40320.0 * N * Math.Pow(Math.Cos(phi), 8.0) * l8coef * Math.Pow(l, 8.0));
        return;
    }

对于co,它是一个结构体

 public struct Coord
    {
        public double zone;
        public double[] eastNorth;

    }

我在这里错过了什么?

谢谢

系统.c#建议

发生这种情况是因为函数中的xy数组(来自Coord.eastNorth)未初始化。我注意到Coord是一个结构体而不是一个类。有什么特别的原因吗?如果Coord是一个类

,这将工作。
public class Coord
{
    public double Zone {get; set;}
    public List<double> EastNorth {get; set;}
    public Coord()
    {
        EastNorth = new List<double>();
    }
}

public List<double> MapLatLonToXY(double phi, double lambda, double lambda0)
{
   var xy = new List<double>();
   //Exception for xy[0] ....!
    xy.Add(N * Math.Cos(phi) * l
        + (N / 6.0 * Math.Pow(Math.Cos(phi), 3.0) * l3coef * Math.Pow(l, 3.0))
        + (N / 120.0 * Math.Pow(Math.Cos(phi), 5.0) * l5coef * Math.Pow(l, 5.0))
        + (N / 5040.0 * Math.Pow(Math.Cos(phi), 7.0) * l7coef * Math.Pow(l, 7.0)));
    /* Calculate northing (y) */
    xy.Add(ArcLengthOfMeridian(phi)
        + (t / 2.0 * N * Math.Pow(Math.Cos(phi), 2.0) * Math.Pow(l, 2.0))
        + (t / 24.0 * N * Math.Pow(Math.Cos(phi), 4.0) * l4coef * Math.Pow(l, 4.0))
        + (t / 720.0 * N * Math.Pow(Math.Cos(phi), 6.0) * l6coef * Math.Pow(l, 6.0))
        + (t / 40320.0 * N * Math.Pow(Math.Cos(phi), 8.0) * l8coef * Math.Pow(l, 8.0)));
    return xy;
}
public Coord LatLonToUTMXY(double lat, double lon)
{
    Coord co = new Coord();
    co.zone = Math.Floor((lon + 180.0) / 6) + 1;
    //double zone = Math.Floor((lon + 180.0) / 6) + 1;
    co.EastNorth = MapLatLonToXY(DegToRad(lat), DegToRad(lon), UTMCentralMeridian(co.zone));
    /* Adjust easting and northing for UTM system. */
    co.EastNorth[0] = co.EastNorth[0] * UTMScaleFactor + 500000.0;
    co.EastNorth[1] = co.EastNorth[1] * UTMScaleFactor;
    if (co.EastNorth[1] < 0.0)
        co.EastNorth[1] = co.EastNorth[1] + 10000000.0;

    return co;
}