Question

This is the structure im trying to work with:

Public Structure Point
        Public Property id As Integer
        Public Property x As Integer
        Public Property y As Integer

        Public Property Points As List(Of Point)

        Public ReadOnly Property key As String
            Get
                Return x & "," & y
            End Get
        End Property

        Public Sub Add(t As Point)
            If Points Is Nothing Then
                Points = New List(Of Point)
            End If
            Points.Add(t)
        End Sub
End Structure

The following code below is an attempt to use the Point Structure:

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?

SOLVED: seems the following code works:

Public Structure Point
    Public Property id As Integer
    Public Property x As Integer
    Public Property y As Integer

    Private _points As List(Of Point)
    Public ReadOnly Property Points() As List(Of Point)
        Get
            If _points Is Nothing Then
                _points = New List(Of Point)
            End If

            Return _points
        End Get
    End Property

    Public ReadOnly Property key As String
        Get
            Return x & "," & y
        End Get
    End Property

    Public Sub Add(t As Point)
        If _points Is Nothing Then
            _points = New List(Of Point)
        End If
        _points.Add(t)
    End Sub
End Structure

usage:

Dim l = New List(Of Point)
Dim t = new Point 'test point

t.add(t)
l.Add(t)
Was it helpful?

Solution

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.

OTHER TIPS

Everytime you access the point property you need to check if it's Nothing like you did in the add function.

Your best bet would be to put a constructor to initialize the list (you might need to change it to a class). You could also have a initialize function that would be class everytime you create a new instance.

Public Structure Point

        Public Sub Initialize()
            Points = New List(Of Point)
        End Sub

        ...

End Structure

I just notice the other comment. You could just have a property that initialize a private variable if it's still Nothing.

    Private _points As List(Of Point)

    Public ReadOnly Property Points() As List(Of Point)
        Get
            If _points Is Nothing Then
                _points = New List(Of Point)
            End If

            Return _points
        End Get
    End Property
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top