4

I know how to create an array and loop through it normally - but what if I need a multi-column array. e.g. 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?

2
  • Your second example looks perfectly feasible. What type of object exactly is NameofArray? Some kind of strongly typed array? Commented Apr 11, 2011 at 13:00
  • Well, that is what I need to figure out. I know how to set up an array with one column - but can it have two columns? e.g. I can do Dim my_array as array ("hello", "goodbye", "adios") easily, but how would I do it with two columns, e.g. Dim my_array as array ("hello" "1", "goodbye" "4", "adios" "0")? Commented Apr 11, 2011 at 13:09

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

The 'new' keyword isn't a big deal, and a class is probably a better choice.
Looks like a VB Question to me
I see, so read-only go with structure, read-write go with creating a new class?
Structure is a value type, Class is a reference type. If you want to specify extra stuff, that classes can do go with a class.
@davemackey you can do both with structs and with classes. Its just the foreach loop, which enables you to access only a copy of the real struct elements - due to the nature of structs. If you HAVE to rely on foreach loops, a class would be better suited. Otherwise, you can use an struct array as well. But the elements would be writeable by direct indexing only, like in my example above.
3

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.

Comments

2

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

Comments

1

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);
}

3 Comments

I have not tried the Dictionary class...do you think this is a better option than structure?
I looked into the Dictionary class a bit more and don't think this is what I'm looking for in this particular class. For example, I want to have multiple pairs of name/age, not unique keys.
It will be a great option, if you have only two values. And it supports various find and add methods.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.