Results 1 to 7 of 7

Thread: [2008] Create an array and add items to it

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    8

    [2008] Create an array and add items to it

    I would like to create an array named ingredients, and add items to the array based upon certain criteria. Then I want to use the array to populate a listbox with the ingredients.

    Something like this:
    Dim ingredients as arraylist = new arraylist

    ingredients.Add = (oilamt(x) & " " & measure & " " oilname(x))
    lstIngredients.items = ingredients()

    This section of code is part of a larger part where x is defined and this would be inside a loop to add all items necessary.

    I can't figure out whether to use array or arraylist, first of all, and how to make it work. Thanks for any help!

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2008] Create an array and add items to it

    Hey,

    What it comes down to is what kind of objects are you going to store in the "array". An ArrayList is a general purpose collection that will let you add any object to it. If you know ahead of time what kind of object you are going to be adding, you might want to use a more specific type of collection, or perhaps a generic collection.

    In the code that you have posted, are you getting an error?

    Remember to wrap your code in [CODE] tags to make it more readable.

    Gary

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2008] Create an array and add items to it

    A much easier solution would be a List (of String) in this case (a generic collection type). You can use arrays, but you have to manually re-size them, which is more tricky than the List. You can add to Lists by just calling List.Add. You can also add into specific places in the list, remove items with RemoveAt, etc. Most likely a better solution than just the array.

    You can also add the List directly to the listbox, though offhand, I have forgotten how. Something like AddRange.
    My usual boring signature: Nothing

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2008] Create an array and add items to it

    Hey,

    How about this:

    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim list As New List(Of String)
            list.Add("one")
            list.Add("two")
            list.Add("three")
    
            ListBox1.DataSource = list
        End Sub
    Gary

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2008] Create an array and add items to it

    Although,

    This would work too

    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim list As New List(Of String)
            list.Add("one")
            list.Add("two")
            list.Add("three")
    
            ListBox1.Items.AddRange(list.ToArray)
        End Sub
    Gary

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    8

    Re: [2008] Create an array and add items to it

    Thank you so much for your help. I am very grateful for the advice.

    I figured out a way to get the information into the listbox directly by assigning the ingredients to a string called ingredient first, then adding the item to the list box.

    Code:
    Dim ingredient as String = ""
    ingredient = (oilamt(x) & " " & measure & " " oilname(x))
    lstIngredients.Items.Add(ingredient)

  7. #7
    Lively Member
    Join Date
    Jan 2007
    Posts
    96

    Re: [2008] Create an array and add items to it

    Quote Originally Posted by melaniecarr23
    Thank you so much for your help. I am very grateful for the advice.

    I figured out a way to get the information into the listbox directly by assigning the ingredients to a string called ingredient first, then adding the item to the list box.

    Code:
    Dim ingredient as String = ""
    ingredient = (oilamt(x) & " " & measure & " " oilname(x))
    lstIngredients.Items.Add(ingredient)
    You can shorten that to 1 line of code:

    Code:
    lstIngredients.Items.Add((oilamt(x) & " " & measure & " " oilname(x)))

Posting Permissions

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



Click Here to Expand Forum to Full Width