如何向类添加对象列表 [英] How do I add a list of objects to a class

查看:70
本文介绍了如何向类添加对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我现在已经解决了这个问题几天了,我正在努力解决这个问题。我想要存档的是在另一个类中有一个类的列表。以下是班级。



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.

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.


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





我在此代码的最后一行收到NullReferenceException 。



任何和所有帮助都非常苛刻



我尝试过:



我试过谷歌搜索我想要实现的东西,但我找不到任何对我有意义的东西。



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.

推荐答案

Naccount.Accounts本身不是你可以添加的List,它是一个你可以分配(引用a)列表的属性(m_Accounts是你可以添加的列表,但它是私有的,尚未实例化(已创建))。



您的代码似乎不太有用,但此示例可能会帮助您解决问题:

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:
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


这篇关于如何向类添加对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆