简体   繁体   中英

Create a VB.NET Array with two columns of values?

I know how to create an array and loop through it normally - but what if I need a multi-column array. eg usually I might do something like:

For Each row in NameofArray
  Dim name as String = row
  Response.Write("Hello " & name & "!")
Next

But what if I want to do something like:

For Each row in NameofArray
   Dim name as String = row.name
   Dim age as Integer = row.age
   Response.Write("Hello " & name & "! You are " & age & " years old!"
Next

If this isn't possible with an array, is there another way I can accomplish this?

Create your custom data type:

 public struct DataType 
    public string Name; 
    public int Age; 
 }

Such type you can than use in an array like that:

 DataType[] myData = new DataType[100]; 
 myData[0].Name = "myName"; 
 myData[0].Age = 100; 

Note, if looping through that array via foreach, the elements returned for each iteration cannot get altered. If this is an requirement for you, consider using 'class' rather than 'struct' in the above DataType declaration. This will come with some other implications though. For example, the instances of a class DataType will explicitely have to be created via the 'new' keyword.

After reading your comment I think my other answer is probably what you are looking for.

What type is row and what type is NameOfArray?

If you would like to make row into a coumpound type with several members then there a several options.

Structure Row
   Public Name as String
   Public Age as Integer
End Structure

for instance. If you would prefer a reference type substitute Class for Structure .

Or using anonymous types,

Dim row = New With {Name = "Bob", Age = 21}

Then you can use generics to make a list of rows that you can iterate through using ForEach .

Dim NameOfList As System.Collections.Generic.List(of Row)

or if it were a result of a LINQ query somthing that supported

IEnumerable(of New With{Name As String, Age As Int}). //Not sure if this is VB

I'm not certain I uderstand your question and hope this is the kind of thing you were looking for.

As you can see from my fellow answerers, the support for anonymous types is superior in C# but, since you asked the question in VB.Net I will limit myself to that context.

Did you tried Dictionary Class . You can loop through the Dictionary using KeyValue pair class.

// Create a new dictionary of strings, with string keys.
//
Dictionary<string, string> openWith = 
    new Dictionary<string, string>();

// Add some elements to the dictionary. There are no 
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");


foreach(var item in openWith)
{
Console.WriteLine(item.Key +" can be open with " + item.value);
}

You need to (can) index into your array using the two dimensions ie...

Dim array(,) As Object = { _
     {"John",26}, _
     {"Mark",4} _
}

For row As Integer = 0 to array.GetUpperBound(0)
   Dim name as String = CStr(array(row,0))
   Dim age as Integer = CInt(array(row,1))
   Response.Write("Hello " & name & "! You are " & age & " years old!")
Next

Though would be better storing this sort of information in a class or user defined type of some kind.

After reading your comment I think I understand the question.

You can do

///Spacer Top
Dim NameOfArray = {New With {.Age = 21, .Name = "Bob"}, New With {.Age = 74, .Name = "Gramps"}}
///Spacer Bottom

If you want to create an IEnumberable anonymous type of Name Age tuples ;-p

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