Results 1 to 10 of 10

Thread: Populate an array with data from variables

  1. #1
    keeps21 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Nov 2008
    Posts
    8
    Reputation
    0

    Populate an array with data from variables

    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
    Code:
    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
    Code:
    MyArray(i)= New String { (var1), (var2), (var3), (var4), (var5), (var6) }
    How would I do this?

    Any help is greatly appreciated.
    Last edited by keeps21; 11-28-2008 at 12:24 PM.

  2. #2
    MattP is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    WY, USA
    Posts
    1,206
    Reputation
    572
    This worked for me:

    Code:
    		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}

  3. #3
    keeps21 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Nov 2008
    Posts
    8
    Reputation
    0
    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.

    Code:
    '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?

  4. #4
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,225
    Reputation
    2370
    Use a List(Of T) dynamic collection and its ToArray method. Pseudo:
    Code:
    dim l as new list(of string)
    loop: l.Add("some string")
    return l.ToArray()

  5. #5
    keeps21 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Nov 2008
    Posts
    8
    Reputation
    0
    Doing it this way will I then be able to create an array from the values in this list as per my original post?

  6. #6
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,225
    Reputation
    2370
    Quote Originally Posted by keeps21 View 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?

  7. #7
    keeps21 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Nov 2008
    Posts
    8
    Reputation
    0
    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?

  8. #8
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,225
    Reputation
    2370
    Why would you want each array item to be an array containing only one item? What are you trying to do?

  9. #9
    keeps21 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Nov 2008
    Posts
    8
    Reputation
    0
    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.

  10. #10
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,225
    Reputation
    2370
    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.
    Code:
    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:
    Code:
    Dim s()() As String = jagged.ToArray()

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking