Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Visual Basic 2008 Express Edition!
Posted

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

Visual Basic 2008 Express Edition!
Posted
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.

Posted
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

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
  • *Experts*
Posted

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

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Posted

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

Experience is something you don't get until just after the moment you needed it
  • *Experts*
Posted

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.

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Posted (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 by vbMarkO
Visual Basic 2008 Express Edition!
  • *Experts*
Posted

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

"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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...