C#List添加新元素时要覆盖的元素

时间:2015-10-09 14:38:41

标签: c# parsing

所以我在使用.Add()调用向List添加新对象时遇到问题。

private static List<PointObject> _rawCoordinates = new List<PointObject>();    
private static void KMLParser(string fileLocation)
    {
        string coordinateString = "";

        XDocument document = XDocument.Load(fileLocation);
        XNamespace ns = "http://www.opengis.net/kml/2.2";

        var placemarks = document.Descendants(ns + "LineString")
                      .Select(p => new
                      {
                          Coord = p.Element(ns + "coordinates")
                      })
                      .ToList();

        foreach(var obj in placemarks)
        {
            coordinateString = Regex.Replace(obj.Coord.Value, @"\t|\n|\r| ", "");
            coordinateString = Regex.Replace(coordinateString, ",0", ",0\r");

            foreach (string substr in coordinateString.TrimEnd('\r').Split('\r'))
            {
                _rawCoordinates.Add(LineParser(substr));
            }
        }            
    }
private static PointObject LineParser(string Coordinates)
    {
        PointObject pt = new PointObject();
        int remainder;
        int index = 0; 

        foreach (string substr in Coordinates.TrimEnd('\n').Split(',', '\n'))
        {                
            remainder = index % 3;
            switch (remainder)
            {
                case 0:
                {
                    pt.SetLongitude(Math.Round(Convert.ToDouble(substr), _round));
                    Debug.WriteLine("Longitude: {0}", pt.GetLongitude());
                    index++;
                    break;
                }
                case 1:
                {
                    pt.SetLatitude(Math.Round(Convert.ToDouble(substr), _round));
                    Debug.WriteLine("Latitude: {0}", pt.GetLatitude());
                    index++;
                    break;
                }
                default:
                {
                    pt.SetBearing(0);
                    pt.SetDistance(0);
                    pt.SetIsStop(false);
                    pt.SetLayover(0);
                    break;
                }
            }
        }
        return pt;
    }

正如您所看到的,我将从LineParser方法返回的PointObject添加到_rawCoordinates List,它被定义为List。我每次调用LineParser方法时都会初始化一个新的PointObject,而我发现的所有内容都应该处理这个问题。但是,每次从该方法获取返回值时,它都会覆盖_rawCoordinates List中的所有元素。我不太确定我在这里做错了什么。任何帮助将不胜感激。

编辑:添加我对PointObject的定义

class PointObject
{
    public static double _bearing = 0;
    public static double _distance = 0;
    public static double _longitude = 0;
    public static double _latitude = 0;
    public static bool _isStop = false;
    public static int _layoverLength = 0;

    public PointObject()
    {
        _longitude = 0;
        _latitude = 0;
        _bearing = 0;
        _distance = 0;
        _isStop = false;
        _layoverLength = 0;
    }

    public PointObject(double lon, double lat, double dist, double bear, bool stop, int layover)
    {
        _longitude = lon;
        _latitude = lat;
        _distance = dist;
        _bearing = bear;
        _isStop = stop;
        _layoverLength = layover;
    }

}

所以我假设你要我将那些从公共静态变为公共的,对吧?如果是这样,我可以尝试一下。

0 个答案:

没有答案