Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27355

Populating Class List - VS2008

$
0
0
Hi,

I have a simple Class and a ClassList. When I try to populate the class list, I get a NullReferenceException error. Probably easiest to explain with a small code sample

Code:


    Public Class Customer
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set(ByVal value As String)
                m_Name = value
            End Set
        End Property
        Private m_Name As String
        Public Property Age() As Integer
            Get
                Return m_Age
            End Get
            Set(ByVal value As Integer)
                m_Age = value
            End Set
        End Property
        Private m_Age As Integer
    End Class

    Public Class CustomerList
        Public Property lstCustomer() As List(Of Customer)
            Get
                Return m_lstCustomer
            End Get
            Set(ByVal value As List(Of Customer))
                m_lstCustomer = value
            End Set
        End Property
        Private m_lstCustomer As List(Of Customer)
    End Class


    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        'Populate class and show results in message box
        Dim CustList As New CustomerList
        '**************NullReferenceException error on line below ****************
        CustList.lstCustomer(0).Name = "John"

        CustList.lstCustomer(0).Age = 21
        CustList.lstCustomer(1).Name = "Peter"
        CustList.lstCustomer(1).Age = 25
        Dim i As Integer
        For i = 0 To CustList.lstCustomer.Count - 1
            MsgBox(CustList.lstCustomer(i).Name & "-" & CStr(CustList.lstCustomer(i).Age))
        Next
    End Sub

Thanks a lot in advance :)

Viewing all articles
Browse latest Browse all 27355

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>