C# List Elements being overwritten when adding a new element

micsea64

So I am have an issue with adding a new object to a List using the .Add() call.

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;
    }

So as you can see, I am add the returned PointObject from the LineParser Method,to the _rawCoordinates List, which is defined as a List. I am initializing a new PointObject each time I call the LineParser method, and everything I found, that should have taken care of the issue. However, each time I get a returned value from that method, it overwrites all elements in my _rawCoordinates List. I am not quite sure what I am doing wrong here. Any help would be greatly appreciated.

Edit: Adding my definition of 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;
    }

}

So I am assuming you are going to want me to change those from public static to just public, right? If so, I can give it a try.

AngularRat

This is in the comments above from D Standly and Johny Tee, but you shouldn't have properties that you want to have multiple copies of declared as static in your PointObject class. Static means there is one copy of the field shared by all instances of the class (loose definition, see msdn for more details. (https://msdn.microsoft.com/en-us/library/98f28cdx.aspx)

So, change your PointObject definition to be something like:

class PointObject
{
    public double _bearing = 0;
    public double _distance = 0;
    public double _longitude = 0;
    public double _latitude = 0;
    public bool _isStop = false;
    public 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;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java previous elements in ArrayList/List being overwritten when changing next element

From Dev

C array being overwritten/deleted? very confused

From Dev

Visual C# 2013 - Element disappearing when adding a new one in WPF form editor

From Dev

Python - List elements being overwritten somehow

From Dev

C# - Set that returns index when adding new element

From Dev

Linked list's data being overwritten

From Dev

c# nameof original parameter is being overwritten

From Dev

C# Nested List not adding a new list

From Dev

Good way to keep a list of lists sorted on second element when appending new elements?

From Dev

Adding a new object to List then the already stored objects will be overwritten by the new one

From Dev

function of adding a new element to list

From Dev

Java previous elements in ArrayList/List being overwritten when changing next element

From Dev

dictionary values are being overwritten when adding new key and values

From Dev

How do Refresh List View when adding new elements

From Dev

C array being overwritten/deleted? very confused

From Dev

Issue when adding element into sorted linked list C++

From Dev

Adding elements to a linked list C

From Dev

JSF java list getting overwritten when new item is added

From Dev

Adding element to list deletes previous elements

From Dev

C Array being overwritten?

From Dev

C++ Why is file not being appended or overwritten?

From Dev

Adding new moodle UI elements according to the onchange event of a select element

From Dev

c# nameof original parameter is being overwritten

From Dev

Lists elements are overwritten while adding a list to a list

From Dev

When adding a new object to the list all the objects already added previously are overwritten

From Dev

C++: How to make "core" dump file being overwritten when new crash is encountered?

From Dev

New object is being overwritten by previous object

From Dev

Adding new elements to a List in C# is changing the elements added before

From Dev

'AttributeError' when adding elements to a Python list

Related Related

  1. 1

    Java previous elements in ArrayList/List being overwritten when changing next element

  2. 2

    C array being overwritten/deleted? very confused

  3. 3

    Visual C# 2013 - Element disappearing when adding a new one in WPF form editor

  4. 4

    Python - List elements being overwritten somehow

  5. 5

    C# - Set that returns index when adding new element

  6. 6

    Linked list's data being overwritten

  7. 7

    c# nameof original parameter is being overwritten

  8. 8

    C# Nested List not adding a new list

  9. 9

    Good way to keep a list of lists sorted on second element when appending new elements?

  10. 10

    Adding a new object to List then the already stored objects will be overwritten by the new one

  11. 11

    function of adding a new element to list

  12. 12

    Java previous elements in ArrayList/List being overwritten when changing next element

  13. 13

    dictionary values are being overwritten when adding new key and values

  14. 14

    How do Refresh List View when adding new elements

  15. 15

    C array being overwritten/deleted? very confused

  16. 16

    Issue when adding element into sorted linked list C++

  17. 17

    Adding elements to a linked list C

  18. 18

    JSF java list getting overwritten when new item is added

  19. 19

    Adding element to list deletes previous elements

  20. 20

    C Array being overwritten?

  21. 21

    C++ Why is file not being appended or overwritten?

  22. 22

    Adding new moodle UI elements according to the onchange event of a select element

  23. 23

    c# nameof original parameter is being overwritten

  24. 24

    Lists elements are overwritten while adding a list to a list

  25. 25

    When adding a new object to the list all the objects already added previously are overwritten

  26. 26

    C++: How to make "core" dump file being overwritten when new crash is encountered?

  27. 27

    New object is being overwritten by previous object

  28. 28

    Adding new elements to a List in C# is changing the elements added before

  29. 29

    'AttributeError' when adding elements to a Python list

HotTag

Archive