Transferring items from a listbox to a string variable [modified]
-
Hello. I'm having a little problem transferring all the items in the listbox to a string variable. All the items in the listbox are also string type so i don't have any problems about data types. I tried to use array of string type variable but when i transfer them using a for..loop statement it gets a run-time error that the index was out of bounds. How will I resolve that? How can i store all the items into a variable without selecting them? I used the listbox as a control for outputs. this is the code:
for (int a = 0; a < lstBoxRequests.Items.Count; a++)
{
string[] passedRequests = new string[a];passedRequests\[a\] = Convert.ToString(lstBoxRequests.Items\[a\]); MessageBox.Show(passedRequests\[a\]); }
modified on Friday, November 6, 2009 6:41 AM
-
Hello. I'm having a little problem transferring all the items in the listbox to a string variable. All the items in the listbox are also string type so i don't have any problems about data types. I tried to use array of string type variable but when i transfer them using a for..loop statement it gets a run-time error that the index was out of bounds. How will I resolve that? How can i store all the items into a variable without selecting them? I used the listbox as a control for outputs. this is the code:
for (int a = 0; a < lstBoxRequests.Items.Count; a++)
{
string[] passedRequests = new string[a];passedRequests\[a\] = Convert.ToString(lstBoxRequests.Items\[a\]); MessageBox.Show(passedRequests\[a\]); }
modified on Friday, November 6, 2009 6:41 AM
-
Hello. I'm having a little problem transferring all the items in the listbox to a string variable. All the items in the listbox are also string type so i don't have any problems about data types. I tried to use array of string type variable but when i transfer them using a for..loop statement it gets a run-time error that the index was out of bounds. How will I resolve that? How can i store all the items into a variable without selecting them? I used the listbox as a control for outputs. this is the code:
for (int a = 0; a < lstBoxRequests.Items.Count; a++)
{
string[] passedRequests = new string[a];passedRequests\[a\] = Convert.ToString(lstBoxRequests.Items\[a\]); MessageBox.Show(passedRequests\[a\]); }
modified on Friday, November 6, 2009 6:41 AM
How manu elements are there in your array? And how many elements would the loop go through? If your array holds 10 items, then a for-next loop will go from either 1 to 10 or from 0 to 9. Your loop goes 11 times, starting at passedRequest[0] and moving on to passedRequst[10], generating an "index out of bounds". That means that the number between the [] is smaller or larger then the number of items in the array :)
for (int a = 0; a < lstBoxRequests.Items.Count -1; a++)
Next item is that you're reinitializing the array on each loop. That means that the entire Array is created anew, with a new maximum-upperbound of "a". You'd prolly want a maximum number in the array that corresponds to the number of items in the combobox. That would come to something like this;
int itemCount = lstBoxRequests.Items.Count -1;
string[] passedRequests = new string[itemCount];
for (int a = 0; a < itemCount; a++)
{
passedRequests[a] = Convert.ToString(lstBoxRequests.Items[a]);There are easier ways to enumerate through the items, but I'll leave that to the next poster :)
I are Troll :suss:
-
How manu elements are there in your array? And how many elements would the loop go through? If your array holds 10 items, then a for-next loop will go from either 1 to 10 or from 0 to 9. Your loop goes 11 times, starting at passedRequest[0] and moving on to passedRequst[10], generating an "index out of bounds". That means that the number between the [] is smaller or larger then the number of items in the array :)
for (int a = 0; a < lstBoxRequests.Items.Count -1; a++)
Next item is that you're reinitializing the array on each loop. That means that the entire Array is created anew, with a new maximum-upperbound of "a". You'd prolly want a maximum number in the array that corresponds to the number of items in the combobox. That would come to something like this;
int itemCount = lstBoxRequests.Items.Count -1;
string[] passedRequests = new string[itemCount];
for (int a = 0; a < itemCount; a++)
{
passedRequests[a] = Convert.ToString(lstBoxRequests.Items[a]);There are easier ways to enumerate through the items, but I'll leave that to the next poster :)
I are Troll :suss:
I think you'll miss one item with this code.
Eddy Vluggen wrote:
for (int a = 0; a < lstBoxRequests.Items.Count -1; a++)
or do this:
for (int a = 0; a <= lstBoxRequests.Items.Count -1; a++)
or this:
for (int a = 0; a < lstBoxRequests.Items.Count; a++)
V.
Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive -
How manu elements are there in your array? And how many elements would the loop go through? If your array holds 10 items, then a for-next loop will go from either 1 to 10 or from 0 to 9. Your loop goes 11 times, starting at passedRequest[0] and moving on to passedRequst[10], generating an "index out of bounds". That means that the number between the [] is smaller or larger then the number of items in the array :)
for (int a = 0; a < lstBoxRequests.Items.Count -1; a++)
Next item is that you're reinitializing the array on each loop. That means that the entire Array is created anew, with a new maximum-upperbound of "a". You'd prolly want a maximum number in the array that corresponds to the number of items in the combobox. That would come to something like this;
int itemCount = lstBoxRequests.Items.Count -1;
string[] passedRequests = new string[itemCount];
for (int a = 0; a < itemCount; a++)
{
passedRequests[a] = Convert.ToString(lstBoxRequests.Items[a]);There are easier ways to enumerate through the items, but I'll leave that to the next poster :)
I are Troll :suss:
for (int a = 0; a < lstBoxRequests.Items.Count; a++)
Is right, your loop:
for (int a = 0; a < lstBoxRequests.Items.Count - 1; a++)
is wrong it leaves one item alone! General: for(int loop=0; loop < count; loop++) or for(int loop=0; loop <= count -1; loop++)! His problem was the following:
for (int a = 0; a < lstBoxRequests.Items.Count; a++)
{
string[] passedRequests = new string[a];passedRequests\[a\] = Convert.ToString(lstBoxRequests.Items\[a\]); <---- this causes the out of array, because passedRequests.length = a but passedRequests\[a\] is wrong !!! MessageBox.Show(passedRequests\[a\]); }
Edit: Second problem was the creation of the string array in the loop.
Greetings Covean
-
for (int a = 0; a < lstBoxRequests.Items.Count; a++)
Is right, your loop:
for (int a = 0; a < lstBoxRequests.Items.Count - 1; a++)
is wrong it leaves one item alone! General: for(int loop=0; loop < count; loop++) or for(int loop=0; loop <= count -1; loop++)! His problem was the following:
for (int a = 0; a < lstBoxRequests.Items.Count; a++)
{
string[] passedRequests = new string[a];passedRequests\[a\] = Convert.ToString(lstBoxRequests.Items\[a\]); <---- this causes the out of array, because passedRequests.length = a but passedRequests\[a\] is wrong !!! MessageBox.Show(passedRequests\[a\]); }
Edit: Second problem was the creation of the string array in the loop.
Greetings Covean
Covean wrote:
or (int a = 0; a < lstBoxRequests.Items.Count - 1; a++) is wrong it leaves one item alone!
You're right, I was wrong. I've been looking at these too much;
for(int x = 0; x <= bla.Count - 1; x++)
Notice that the check has an equals-sign. The current project that I'm working has both styles mixed throughout the code. In VB6 :)
I are Troll :suss:
-
Covean wrote:
or (int a = 0; a < lstBoxRequests.Items.Count - 1; a++) is wrong it leaves one item alone!
You're right, I was wrong. I've been looking at these too much;
for(int x = 0; x <= bla.Count - 1; x++)
Notice that the check has an equals-sign. The current project that I'm working has both styles mixed throughout the code. In VB6 :)
I are Troll :suss:
Eddy Vluggen wrote:
In VB6
that explains it :-D
V.
Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive