简体   繁体   中英

VB How do I make an array of a user defined type? (Fields)

I have forgotten how to make an array or whatever out of 8 fields. It looks something like Array.Subcategory[x] afterwards but i've forgotten what its called and how to do it. It needs to be 1 array with multiple entries but each entry have 8 sub entries.

So for a group of people

1 array entry will contain:

Name
Eye colour
Haircolour
Age
Balls
Smell
??? 

One array will contain all this data for each array entry.

cheers

You could define a class in VB.NET like this:

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

Then create a List or Array holding entries like this:

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

public ArrayOfEntries(10) as Entry

Use it like this:

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

Another possibility would be to use a 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

You can do it with types also, which avoids some of the initialization hassels with classes in arrays:

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

Dim bar() As foo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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