Populate an array with data from variables

keeps21

Member
Joined
Nov 28, 2008
Messages
8
Programming Experience
1-3
I have an array.

I want to add the values of 6 variables into this array, however I can't find a way to do it.


If I knew the values I'd do it like this
VB.NET:
MyArray(i)= New String { ("id"), ("ans1"), ("ans2"), ("ans3"), ("ans4"), ("ans5") }

However i don't know the values, but have 6 variables I need to insert into the array.

They are
var1, var2, var3, var4, var5, var6

Pseudocode i've tried for this is
VB.NET:
MyArray(i)= New String { (var1), (var2), (var3), (var4), (var5), (var6) }

How would I do this?

Any help is greatly appreciated.
 
Last edited:
This worked for me:

VB.NET:
		Dim var1 As String = "Variable 1"
		Dim var2 As String = "Variable 2"
		Dim var3 As String = "Variable 3"
		Dim var4 As String = "Variable 4"
		Dim var5 As String = "Variable 5"
		Dim var6 As String = "Variable 6"

		Dim MyArray() As String = {var1, var2, var3, var4, var5, var6}
 
Thanks.

Another thing is I need the variables to be 'populated' dynamically.

The number of variables depends on the number of records returned from the database.

Is it possible to create x number of variables on the fly.

VB.NET:
'number of records returned by query
number_of_records = 5  ' we'll assume it's 5 for this example

'create the variables

For i=0 To number_of records ' which in this case is 5
         
        dim var(i) as object = "some value" ' create variable and assign value

Next

is it possible to do it in this way?
 
Use a List(Of T) dynamic collection and its ToArray method. Pseudo:
VB.NET:
dim l as new list(of string)
loop: l.Add("some string")
return l.ToArray()
 
Doing it this way will I then be able to create an array from the values in this list as per my original post?
 
Doing it this way will I then be able to create an array from the values in this list as per my original post?
Isn't that blinding obvious? What do you think ToArray does?
 
Sorry. I'm getting myself confused.

I'll try to explain what I'm trying to do a little better.

I'm trying to create a multidimensional array, called MyArray

I want to read from the database and add a new array into MyArray for each record in the database so that I can then use it later on in the code.

What would be the best way to achieve this?
 
Why would you want each array item to be an array containing only one item? What are you trying to do?
 
There is an unlimited number of answers.

There may be one answer for each person, there may be 5, this is the reason I want to use a multidimensional array.

I'm then going to display a table

------------------------------
|id|ans1|ans2|ans3|ans4|ans5|
-------------------------------
|1|person1-answer1|person1-answer2|person1-answer3|person1-answer4|person1-answer5|

|2|person2-answer1|person2-answer2|person2-answer3|person2-answer4|person2-answer5|

|3|person3-answer1|person3-answer2|person3-answer3|person3-answer4|person3-answer5|

and so on.
 
It isn't any different from my other example, but how you would do it depends on whether you know all the answers or not when declaring. List collections are used you need to dynamically add items, array may be used when you know the number of items. Here since number of answers vary between persons I'd use List(of list) or jagged array and not multidimensional array.
VB.NET:
Dim jagged As New List(Of String())
jagged.Add(New String() {"1", "answer1", "answer2"})
jagged.Add(New String() {"2", "answer1"})
If needed locking this to a jagged array:
VB.NET:
Dim s()() As String = jagged.ToArray()
 

Latest posts

Back
Top