VB如何创建用户定义类型的数组? (字段)

时间:2014-05-19 19:24:49

标签: arrays vb.net

我忘记了如何制作阵列或8个字段中的任何一个。之后它看起来像Array.Subcategory [x],但我忘记了它的名称以及如何做到这一点。 它需要是1个包含多个条目的数组,但每个条目都有8个子条目。

所以对一群人来说

1个数组条目将包含:

Name
Eye colour
Haircolour
Age
Balls
Smell
??? 

一个数组将包含每个数组条目的所有数据。

欢呼声

2 个答案:

答案 0 :(得分:2)

您可以像这样定义class in VB.NET

public class Entry
    public Name as String
    public EyeColour as String
    public Haircolour as String
    public Age as Integer
    public Balls as Integer
    public Smell as String
end class

然后创建一个ListArray这样的条目:

public ListOfEntries as List(of Entry) = new List(of Entry)

public ArrayOfEntries(10) as Entry

像这样使用:

dim e as Entry = new Entry
e.Name = "Test"
e.EyeColour = "Blue"
' add new object to list
ListOfEntries.Add(e)
' add new object at position 0
ArrayOfEntries(0) = e

另一种可能性是使用Structure (used-defined data type)

' a record of data
public structure Entry
    public Name as String
    public EyeColour as Integer
    '...
end structure
' array of entries
public Entries(10) as Entry
' usage like in the class example
dim e = new Entry
e.Name = "Test"
e.EyeColour = 5
Entries(0) = e

答案 1 :(得分:0)

你也可以使用类型来实现它,这避免了一些带有数组类的初始化hassels:

Type foo
    Name as String
    EyeColour as Integer
    ...
End Type

Dim bar() As foo