|
-
Oct 5th, 2008, 08:25 PM
#1
Thread Starter
New Member
[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!
-
Oct 5th, 2008, 08:44 PM
#2
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
-
Oct 5th, 2008, 08:56 PM
#3
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
 
-
Oct 5th, 2008, 08:59 PM
#4
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
-
Oct 5th, 2008, 09:01 PM
#5
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
-
Oct 5th, 2008, 10:37 PM
#6
Thread Starter
New Member
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)
-
Oct 6th, 2008, 06:35 AM
#7
Lively Member
Re: [2008] Create an array and add items to it
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|