[Resolved]Building string from listbox
Does this code look correct to complete the task?
Code:
'store all medications information as string value
While lstMeds.Items.Count > -1
For i = 0 To lstMeds.Items.Count - 1
strActiveMeds = strActiveMeds & lstMeds.SelectedItem & ", "
Next
End While
Thanks.
Re: Building string from listbox
First off, the while-loop will never reach an end because you arent removing items from the ListBox, thus not changing the lstMeds.Items.Count property.
And even if you did, the Count property could never go lower than 0 so it'll always be greater than -1.
You are also appending the same item to the string in each iteration in the for-loop; the selected item. You'd need to do something like this:
Code:
For i = 0 To lstMeds.Items.Count - 1
strActiveMeds = strActiveMeds & Cstr(lstMeds.Items(i))
Next
However its always a good idea to use a StringBuilder when building strings like this. Search for "StringBuilder" on MSDN and you'll find out how.
Re: Building string from listbox
Re: Building string from listbox
Thanks. I didn't know anything about the StringBuilder class!