Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have been working on this problem for a few days now and am struggling with it. what I am trying to archive is to have a list of a class in another class. below is the class.

VB
Public Class Account
        Public Property Email() As String
            Get
                Return m_Email
            End Get
            Set(ByVal value As String)
                m_Email = Value
            End Set
        End Property
        Private m_Email As String
        Public Property Active() As Boolean
            Get
                Return m_Active
            End Get
            Set(ByVal value As Boolean)
                m_Active = Value
            End Set
        End Property
        Private m_Active As Boolean
        Public Property CreatedDate() As DateTime
            Get
                Return m_CreatedDate
            End Get
            Set(ByVal value As DateTime)
                m_CreatedDate = Value
            End Set
        End Property
        Private m_CreatedDate As DateTime
    End Class
    Public Class ListOfAccounts
        Public Property User() As String
            Get
                Return m_User
            End Get
            Set(ByVal value As String)
                m_User = value
            End Set
        End Property
        Private m_User As String
        Public Property Accounts() As List(Of Account)
            Get
                Return m_Accounts
            End Get
            Set(ByVal value As List(Of Account))
                m_Accounts = value
            End Set
        End Property
        Private m_Accounts As List(Of Account)
    End Class


here is the code that I am using to populate this class.


VB
Dim Naccount As New ListOfAccounts
        Naccount.User = "James"
        For i = 0 To 5
            Dim AccountL As New Account
            AccountL.Active = True
            AccountL.CreatedDate = Now
            AccountL.Email = "james" & i.ToString & "@example.com"
            Naccount.Accounts.Add(AccountL)
        Next


I am getting a NullReferenceException on the last Line of this code.

Any and All help is Very Much apreciated

What I have tried:

I have tried googling what i am trying to achieve and am not finding anything that makes sense to me.
Posted
Updated 7-Jun-17 15:45pm

1 solution

Naccount.Accounts itself is not a List you can add to, it's a property you can assign a (reference to a) List to (m_Accounts is a List you can add to but it's private and not yet instantiated (created)).

Your code seems not very useful but this example might help you solve your problem:
VB
Dim Naccount As New ListOfAccounts
Naccount.User = "James"
Dim AccountList As New List(Of Account)
For i = 0 To 5
    Dim AccountL As New Account
    AccountL.Active = True
    AccountL.CreatedDate = Now
    AccountL.Email = "james" & i.ToString & "@example.com"
    AccountList.Add(AccountL)
Next
Naccount.Accounts = AccountList
 
Share this answer
 
Comments
Aneets 7-Jun-17 22:30pm    
thank you so much Peter.
this was code I wrote to see if I could get the output that I wanted.

I cant thank you enough for your help.

Cheers
[no name] 7-Jun-17 22:42pm    
You're welcome. Thanks for your kind reply.
Cheers
Karthik_Mahalingam 7-Jun-17 23:58pm    
5
[no name] 8-Jun-17 6:13am    
Thank you Karthik!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900