vbMarkO Posted September 15, 2005 Posted September 15, 2005 I have tried everything I can think of and it kills me to have to ask this...... I should know it but for the life of me it is not coming I am drawing a blank and since I am drawing a blank and need it I cant seem to find an example of it... I am google tired so here is what I need I want the items of a listbox into an array I was trying something like this Dim myArr() As String For i As Integer = 0 To list1.Items.Count myArr(i) = list1.Items.Item(i) Next I tried more but cant remember them just the same none of them worked vbMarkO Quote Visual Basic 2008 Express Edition!
JTDPublix Posted September 15, 2005 Posted September 15, 2005 Try this: i = 0 For each existingItem as ListItem in list1.Items myArr(i) = existingItem.Value() i += 1 Next Quote
vbMarkO Posted September 15, 2005 Author Posted September 15, 2005 I got an error that ListItems was not declared also I could not find .value For Each itm As ListItem ' Error ListItem not declared myArr(i) = itm.Value() ' Error .Value was not an option for selection in the drop down list vbMarkO Quote Visual Basic 2008 Express Edition!
JTDPublix Posted September 15, 2005 Posted September 15, 2005 I got an error that ListItems was not declared also I could not find .value For Each itm As ListItem ' Error ListItem not declared myArr(i) = itm.Value() ' Error .Value was not an option for selection in the drop down list vbMarkO ListItem can be found in System.Web.UI.WebControls. Make sure that you have access to that namespace. Value is a property of a ListItem. Quote
techmanbd Posted September 15, 2005 Posted September 15, 2005 I have tried everything I can think of and it kills me to have to ask this...... I should know it but for the life of me it is not coming I am drawing a blank and since I am drawing a blank and need it I cant seem to find an example of it... I am google tired so here is what I need I want the items of a listbox into an array I was trying something like this Dim myArr() As String For i As Integer = 0 To list1.Items.Count myArr(i) = list1.Items.Item(i) Next I tried more but cant remember them just the same none of them worked vbMarkO Try Dim myArr() As String redim myArr(list1.Items.Count) For i As Integer = 0 To list1.Items.Count myArr(i) = list1.Items.Item(i) Next Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
*Experts* DiverDan Posted September 15, 2005 *Experts* Posted September 15, 2005 Since the ListBox collection is also 0 based, you might want to subtract one from it's count properity. Dim myArr(ListBox1.Items.Count - 1) As String Dim i As Integer For i = 0 To ListBox1.Items.Count - 1 myArr(i) = ListBox1.Items.Item(i) Next Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Machaira Posted September 15, 2005 Posted September 15, 2005 Dim iLp As Integer Dim arr(ListBox1.Items.Count - 1) As String For iLp = 0 To ListBox1.Items.Count - 1 arr(iLp) = CType(ListBox1.Items(iLp), String) Next iLp Quote Here's what I'm up to.
travisowens Posted September 19, 2005 Posted September 19, 2005 Some coding 101 here, and as JTDPublix pointed out in code... When you need to get everything in a collection (listbox items, things in an array, objects in a grouping, etc, etc) aslways use ForEach I'd say in normal business programming you should almost never have to use For Quote Experience is something you don't get until just after the moment you needed it
Machaira Posted September 19, 2005 Posted September 19, 2005 I'd say in normal business programming you should almost never have to use For Huh?!? :confused: Quote Here's what I'm up to.
samsmithnz Posted September 19, 2005 Posted September 19, 2005 Some coding 101 here' date=' and as [b']JTDPublix[/b] pointed out in code... I'd say in normal business programming you should almost never have to use For Why? And when did this become programming 101...? Quote Thanks Sam http://www.samsmith.co.nz
IngisKahn Posted September 19, 2005 Posted September 19, 2005 when did this become programming 101...? Damn, I think I'm in the wrong class... Quote "Who is John Galt?"
*Experts* DiverDan Posted September 19, 2005 *Experts* Posted September 19, 2005 Gosh...I thought that both had their place and practically.... Dim myArray(20) As String For Each.??.In myArray Don't need .GetUpperBound anymore it guess. Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
vbMarkO Posted September 20, 2005 Author Posted September 20, 2005 (edited) DOesnt appear everyone is in agreeance with the FOr Each uh statement that was made No Pun intended or implied!! But a question, If the other works why woud it not be as applicable? This again is a noob question. I am self taught, thus, when I see debates like this, I wonder what the argument is. I often make a mistake I think of simply thinking if it works then it must be applicable, however, I know in every application this may not work as in some situations there may be a better or faster way of getting it done. Anyway just wondering .... what is the argument? vbMarkO Edited September 20, 2005 by vbMarkO Quote Visual Basic 2008 Express Edition!
Nate Bross Posted September 20, 2005 Posted September 20, 2005 And the flame wars begin... Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
samsmithnz Posted September 20, 2005 Posted September 20, 2005 I wonder what the argument is. I'm wondering the same thing... Quote Thanks Sam http://www.samsmith.co.nz
*Experts* Nerseus Posted September 20, 2005 *Experts* Posted September 20, 2005 Using a foreach is preferred over a custom for loop. The main reason is readability with a second benefit being it's more "error proof". The times you can't use a foreach is whenever your loop needs to modify the collection/array. Performance used to be a factor: the for loop was traditionally faster. Now, .NET makes the equally as fast. Plus, in most applications the speed of a foreach loop - even if it WERE slower - would be so small that it wouldn't matter. You can read a better argument in Effective C#: 50 Specific Ways to Improve Your C#. The same argument holds for VB.NET. I have only needed a for loop in two circumstances, one by far the more common - deleting rows from a DataTable. The idea is this: DataRow[] matchingRows = ds.Tables[0].Select("column = 'value'"); for(int i=matchingRows.Length - 1; i>=0; i--) { matchingRows.Delete(); } In the above circumstance, you can't use foreach because you're deleting a row inside the loop. The foreach requires that the structure can't be added to or deleted from, but can be changed (sorta). -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.