0

我有一个Class ABC. 我想为它写两个属性。我已经在代码中提到了一个。另一个将是single dimensional array.

Public Class ABC
    Private m_Type As String  
    Private SomeArray........need to write a property for array which will be of type `int`

    Public Property Type() As String
        Get
            Return m_Type
        End Get
        Set(ByVal value As String)
            m_Type = value
        End Set
    End Property  

End Class

我不确定如何为数组定义一个可以在List(Of ABC). 数组的属性可以是只读数组,就像我一样

硬编码它的数据。

所以基本上当我这样做时,

Dim SomeList As New List(Of ABC) 

在for循环中我需要这样的东西,

SomeList.Item(index).SomeArray......this will give me all the items inside the array
4

2 回答 2

2

您可以像声明不同的属性类型一样声明数组属性:

Public Class ABC
    Private _Type As String
    Private _SomeArray As Int32()

    Public Property SomeArray As Int32()
        Get
            Return _SomeArray
        End Get
        Set(ByVal value As Int32())
            _SomeArray = value
        End Set
    End Property

    Public Property Type() As String
        Get
            Return _Type
        End Get
        Set(ByVal value As String)
            _Type = value
        End Set
    End Property
End Class

例如,如果您想Integers在列表的一个数组中循环所有内容:

Dim index As Int32 = 0
Dim someList As New List(Of ABC) 
For Each i As Int32 In someList(index).SomeArray

Next
于 2013-08-19T13:46:28.223 回答
0

如果您不打算在 Gets 和 Sets 中做任何特别的事情,您可以稍微简化代码,如下所示(初始化只读数组以包含数字 1、2、3 和 4):

Public Class ABC
    Public Property Type As String  
    Public ReadOnly Property SomeArray As Integer() = {1,2,3,4}

End Class
于 2013-08-19T14:13:10.813 回答