将 属性 创建为具有属性的列表或数组
Creating a property as a list or array with properties
我想用这些结果创建一个数组或列表 属性:
team(1).name = "Falcons"
team(1).totalPoints = 167
team(2).name = "Jets"
team(2).totalPoints = 121
等等....
我知道如何创建属性,但不知道如何创建数组或列表。谢谢
.net 中没有子属性,但您可以通过创建具有属性的 class 对象列表来实现您的目标。尝试以下操作:
Public Class Team
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Private _TotalPoints As Integer
Public Property TotalPoints() As Integer
Get
Return _TotalPoints
End Get
Set(ByVal value As Integer)
_TotalPoints = value
End Set
End Property
End Class
然后您可以创建 class Team
的对象列表,如下所示:
Dim TeamList As New List(Of Team)
TeamList.Add(New Team() With {.Name = "Falcons", .TotalPoints = 167})
TeamList.Add(New Team() With {.Name = "Jets", .TotalPoints = 121})
所以;
TeamList(0).Name Gives "Falcons"
TeamList(0).TotalPoints Gives 167
TeamList(1).Name Gives "Jets"
TeamList(1).TotalPoints Gives 121
我想用这些结果创建一个数组或列表 属性:
team(1).name = "Falcons"
team(1).totalPoints = 167
team(2).name = "Jets"
team(2).totalPoints = 121
等等....
我知道如何创建属性,但不知道如何创建数组或列表。谢谢
.net 中没有子属性,但您可以通过创建具有属性的 class 对象列表来实现您的目标。尝试以下操作:
Public Class Team
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Private _TotalPoints As Integer
Public Property TotalPoints() As Integer
Get
Return _TotalPoints
End Get
Set(ByVal value As Integer)
_TotalPoints = value
End Set
End Property
End Class
然后您可以创建 class Team
的对象列表,如下所示:
Dim TeamList As New List(Of Team)
TeamList.Add(New Team() With {.Name = "Falcons", .TotalPoints = 167})
TeamList.Add(New Team() With {.Name = "Jets", .TotalPoints = 121})
所以;
TeamList(0).Name Gives "Falcons"
TeamList(0).TotalPoints Gives 167
TeamList(1).Name Gives "Jets"
TeamList(1).TotalPoints Gives 121