简体   繁体   English

VB.NET需要一个class属性作为列表数组

[英]VB.NET need a class property to be a list array

Stack Overflowers: 堆放过量鲜花:

I have been racking my brain trying to get an List(of T) type array to be the property of a class. 我一直在绞尽脑汁试图获取一个List(of T)类型数组作为类的属性。 I know there has to be a simple way of doing it and I can't find a good example on google. 我知道必须有一种简单的方法,但我在Google上找不到很好的例子。 Everytime I create a class that I think will work I get the "Object reference not set to an instance of an object" error when I try to use it. 每次创建我认为可以使用的类时,当我尝试使用它时,都会出现“对象引用未设置为对象的实例”错误。 My thinking now is that I cannot use it in the way I was hoping to. 我现在的想法是,我不能以我希望的方式使用它。 Here is my latest attempt: 这是我最近的尝试:

Public Class Item
    Private _itemno As String
    Public Property ItemNo() As String
        Get
            Return _itemno
        End Get
        Set(ByVal value As String)
            _itemno = value
        End Set
    End Property
        //Many more properties in here
End Class


Public Class Accessory
    Private _items as List(of Item)
    Public Property Items() As List(of Item)
        Get
            Return _itemno
        End Get
        Set(ByVal value As List(of Item))
            _itemno = value
        End Set
    End Property
End Class

Public Class MasterItem
    Private _item as Item
    Public Property PrimaryItem as Item
        Get
            Return _item
        End Get
        Set(ByVal value As Item)
            _item = value
        End Set
    End Property

    Private _accessories as Accessory
    Public Property Accessories() As Accessory
        Get
            Return _accessories
        End Get
        Set(ByVal value As Accessory)
            _accessories = value
        End Set
    End Property
End Class

I am trying to return the MasterItem class from a test function like this: 我试图从这样的测试函数返回MasterItem类:

Public Shared Function GetItem() as MasterItem
    Dim testItem as new MasterItem

    ReturnItem.PrimaryItem.ItemNo = "TEST123"

    ReturnItem.Accessories.Items.add(New Item("TESTACC1"))
    ReturnItem.Accessories.Items.add(New Item("TESTACC2"))

    Return testItem
End Function

What am I doing wrong here? 我在这里做错了什么? Thanks in advance. 提前致谢。

You haven't create an instance of list you are trying to put items to. 您尚未创建要放入项目的列表的实例。

Initialize it in constructor of your Accessory class. 在您的Accessory类的构造函数中对其进行初始化。 Something like 就像是

Public Sub New()
    _items = New List(Of Item)
End Sub

You have 2 options to avoid the NullReferenceException: 您有2个避免NullReferenceException的选项:

1) The approach elder_george mentioned, where you initialize the property to a new instance of the class: 1)提到了elder_george方法,您将属性初始化为该类的新实例:

Dim ReturnItem As New MasterItem
ReturnItem.PrimaryItem = New Item()
' or check for null then initialize (optional, depends on your needs)
If ReturnItem.PrimaryItem Is Nothing Then ReturnItem.PrimaryItem = New Item()

The problem with this approach is if you don't initialize it somewhere that you know it will always be valid to use later, you'll have to do the same check and/or initialize. 这种方法的问题是,如果您不初始化它以后在以后将永远有效的某个地方,则必须执行相同的检查和/或初始化。 That "somewhere" might be on a form load or such. 该“某处”可能在表单加载等上。

2) Initialize the property's backing store variable. 2)初始化属性的后备存储变量。 Do this once and you don't need to check for null all the time. 只需执行一次,您就不必一直检查null。 Notice the change in the _item declaration below: 请注意以下_item声明中的更改:

Public Class MasterItem
    Private _item as Item = New Item()
    Public Property PrimaryItem as Item
        Get
            Return _item
        End Get
        Set(ByVal value As Item)
            _item = value
        End Set
    End Property
End Class

With the above in place, you would access it directly as: 完成上述操作后,您可以通过以下方式直接访问它:

Dim ReturnItem As New MasterItem
ReturnItem.PrimaryItem.ItemNo = "TEST123"

try something like this .. 尝试这样的事情..

Public Class ActiveInfo
    Private _services As List(Of ActiveService)

    Public Sub New()
        _services = New List(Of ActiveService)
    End Sub
    Public Property Services() As List(Of ActiveService)
        Get
            Return _services
        End Get
        Set(ByVal value As List(Of ActiveService))
            _services = value
        End Set
    End Property
End Class

It's working fine with me... 对我来说很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM