Create a VB.NET Array with two columns of values?
Solution 1
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.
Solution 2
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.
Solution 3
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
Solution 4
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);
}
Solution 5
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.
Dave Mackey
Love to code: Python, JS, C#, PHP, SQL, VB.NET, HTML, CSS. In ancient days I coded in QuickBasic, ASP, and VBScript. I'm a friendly introvert with solid communication skills. I work hard and have refined problem solving skills. When I was younger I worked in a variety of industries (commercial fisherman, stone mason, lawn care, factory, custodial, youth leader). I settled on IT and have experience working in a startup, higher education, and with non-profits.
Updated on June 13, 2022Comments
-
Dave Mackey about 2 years
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?
-
Martin about 13 yearsThe 'new' keyword isn't a big deal, and a class is probably a better choice.
-
Jodrell about 13 yearsLooks like a VB Question to me
-
Dave Mackey about 13 yearsI have not tried the Dictionary class...do you think this is a better option than structure?
-
Dave Mackey about 13 yearsI see, so read-only go with structure, read-write go with creating a new class?
-
Dave Mackey about 13 yearsI 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.
-
Anuraj about 13 yearsIt will be a great option, if you have only two values. And it supports various find and add methods.
-
Jodrell about 13 yearsStructure is a value type, Class is a reference type. If you want to specify extra stuff, that classes can do go with a class.
-
user492238 about 13 years@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.