Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I have a snippet :
C#
Thread[] t = new Thread[3];
          string[] items = new string[] {"hi","hello","hey"};


          for(int i = 0 ; i <=t.Length; i++) /// When i hover my mouse here then i = 3
          {
              string s = items[i];/// index out of range exception thrown here

              t[i] = new Thread(()=>work(s));
              t[i].IsBackground = true;
              uplist<string> up = new uplist<string>(t[i]);
              up.Add_item(items[i]);

          }


Does this mean that when i say :

for( ;i<=value;) ///here i is assigned the "value" and so in the next iteration i has reached the maximum value and hence it breaks out and throws an exception ?

The moment the loop starts it throws an exception, where as it should throw an exception when i = 3.

Can someone please elaborate on this?
Posted
Updated 24-Jan-14 16:49pm
v2
Comments
Rahul VB 24-Jan-14 22:50pm    
Please check question modified

You iterate over the items array using indices from t array.

If they both have the same number of items, that works, but it's only by chance.
Better construct t using length of items.

Correct code would be:
C#
string[] items = new string[] {"hi","hello","hey"};
Thread[] t = new Thread[items.length];
for(int i = 0 ; i < t.Length; i++)
{
   string s = items[i];
   t[i] = new Thread(() => work(s));
   t[i].IsBackground = true;
   uplist<string> up = new uplist<string>(t[i]);
   up.Add_item(items[i]);
}
 
Share this answer
 
Replace your for loop by

C#
for (int i = 0; i < t.Length; i++) /// When i hover my mouse here then i = 3
{
           string s = items[i];/// index out of range exception thrown here
}


** Because though there are 3 elements in array.. but by default array starts indexing from 0 position. So if you try to find element at 3 rd position it is giving error.
 
Share this answer
 
C#
 for (int i = 0; i <= t.Length; i++)

replace it to
C#
for (int i = 0; i < t.Length; i++)


if it is <= it will check upto 3 then it will try to access the fourth index of the array, so you got the index exception..
 
Share this answer
 
v2
Comments
Rahul VB 24-Jan-14 5:51am    
Yes eventually i did that. But my doubt is what ever i put as the second item in the for loop like suppose

for(;i <= value;) ////am i also assigning that value? which is why, may be it checks for that value also?
Rahul VB 24-Jan-14 22:50pm    
Hey please check, i modified the question
Karthik_Mahalingam 24-Jan-14 23:29pm    
Hey i am in theatre now, will check after 4 hours.
Karthik_Mahalingam 25-Jan-14 4:47am    
int i=0, value=8;
for (; i <= value; )
{
Console.WriteLine(i);
i++;
}
// output :0,1,2,3,4,5,6,7,8
Here you go:

C#
Thread[] t = new Thread[3];
          string[] items = new string[] {"hi","hello","hey"};


          for(int i = 0 ; i <t.Length; i++) ///  hover your mouse here 
          {
              string s = items[i];/// pls check if index out of range exception thrown here

              t[i] = new Thread(()=>work(s));
              t[i].IsBackground = true;
              uplist<string> up = new uplist<string>(t[i]);
              up.Add_item(items[i]);

          }


Write back if it throws error:)
 
Share this answer
 
or try:-
for(int i = 0 ; i <=t.Length; i++)

to
for(int i = 0 ; i <=t.Length -1; i++)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900