I guess the error you talk about is a NullReferenceException
, and the solution is to create an instance before accessing using the New
keyword.
You already do this in the Add
method, but you should create the instance in the constructor.
Also, you should probably use a Class
if you don't have good reasons to use a mutable Structure
.
In response to your edit:
Dim l = New List(Of Point) Dim t = new Point 'test point l.Add(t) l(l.Count - 1).Add(t)
Looking at l(0).Points in the locals window, the property is always Nothing.
Why is this?
That's why I said don't use a mutable Structure
if you don't have good reasons to.
If you call l.Add(t)
, a copy of t
will be added to l
.
When you access a Point
item via l(l.Count - 1).Add(t)
, you create another copy of the Point
, then add another copy of t
to that second copy.
Add
is called on the second copy of t
. When you look at l(0)
, you actually see a copy of the first copy of t
.
If that is not the behaviour you want, use a class
.