vb.net 将属性创建为具有属性的列表或数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34387412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:39:02  来源:igfitidea点击:

Creating a property as a list or array with properties

vb.netpropertiesvisual-studio-2015.net-4.6

提问by RonB

I would like to create a array or list property with theses results:

我想用这些结果创建一个数组或列表属性:

team(1).name = "Falcons"
team(1).totalPoints = 167
team(2).name = "Jets"
team(2).totalPoints = 121

and so on....

等等....

I know how to make properties, but not as an array or list. Thanks.

我知道如何创建属性,但不是作为数组或列表。谢谢。

回答by sujith karivelil

There is no sub-properties in .net, but you can achieve your target by creating a List of objects of a class that having properties. try the following:

.net 中没有子属性,但您可以通过创建具有属性的类的对象列表来实现您的目标。尝试以下操作:

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

Then you can create a list of objects of the class Teamas follows:

然后,您可以创建类的对象列表,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})

So that ;

以便 ;

TeamList(0).Name         Gives  "Falcons"
TeamList(0).TotalPoints  Gives   167
TeamList(1).Name         Gives   "Jets"
TeamList(1).TotalPoints  Gives   121