Can anybody correct my codes?
I populated a listbox with emails(String).
how do i retrieve the emails from the listbox and store the emails into a studentArray??
string[] studentArray = new string[];
foreach (ListItem email in lbxMpgroup.Items)
{
lblStudent.Text += email.Text + "%26lt;br%26gt;";
for (int i = 0; i %26lt; studentArray.Length; i++)
{
studentArray[i] = email.Text;
}
}
When i run the above code, only the first email is stored in the array....
Thanks! =)
How To Retrieve list of items from listbox in C#?
The assignment to the string array is done within the For Each clause. This means that each time a ListItem value is retrieved, that value will be assigned to studentArray[0]—which is not what you want. You want each ListItem's value to be assigned to the next available studentArray element.
Do you have a studentArray that is large enough to hold all of the ListItem values. What is the upper limit of the studentArray array? How many items can it currently hold?
The assignment to student Array should NOT be done within its own For Loop, because it keep starting all over again—each time a new ListItem value is retrieved (within the outer For Each loop.
Instead the array element assignment should be like so:
studentArray[i] = email.Text;
i++
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment