List generic having strange behavior

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm using List<> to store a class of positions.

Position are basically latitude and longitudes (doubles).
the line is something like

List<position> P = new List<position>();

Then I start adding to the List.
P.add(Positions);

After about 120 entry the List starts writing the enrty at 0 even thou the
list keeps growing. I watched it in the dbeugger overwrite all the values in
the List as it adds the new one.

I made a temp that hold just a double and that works correctly.

Any body having trouble with List of classes?
 
rdh,

This should be relatively easy to reproduce. Can you post a compilable
example of where this happens?
 
Nicholas Paldino said:
rdh,

This should be relatively easy to reproduce. Can you post a compilable
example of where this happens?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

rdh said:
I'm using List<> to store a class of positions.

Position are basically latitude and longitudes (doubles).
the line is something like

List<position> P = new List<position>();

Then I start adding to the List.
P.add(Positions);

After about 120 entry the List starts writing the enrty at 0 even thou the
list keeps growing. I watched it in the dbeugger overwrite all the values
in
the List as it adds the new one.

I made a temp that hold just a double and that works correctly.

Any body having trouble with List of classes?

Well, Not so easy... The lats and longs come from a file of about 5000 points.
but I did notice that some other class with a List<> is doing the same
thing, so I thinks it's me.

What I have is something like
class AC
{
private List<Position> m_pos;

}

class position
{
public double lat;
public double Long;
constructor is overloaded three times here.
}

Now, The class is used on a form as a private attribute of the form and init
isin the constructor. Since, both are doing the same thing I'm think Ive
placed them wrong.

soemthing like

class FORM1
{
private ac a;

public FORM1()
{
a = new ac();
}

public Click_Button()
{
// read the points from the file into a;
// it's here where the List<> starts to overwrite it self.
}

pubilc click_button2()
{
// do soemthing else with the ac
}
}
}

I'm working on a smaller example of the problem, it will take time,
Thanks for your help....
I guess my question is are the list going out-of scope?
Robert
 
What a bone head mistake!!!

I was adding the pointer to the list instead of the objects,

m.add(pos);
should have been
m.add(new position(Lat,long));

of course the debugger was showing all the locations changing they were
pointing to the same object, which I was changing.

Thanks for the help.
 
Back
Top