index was outside the bounds

  • Thread starter Thread starter rodchar
  • Start date Start date
R

rodchar

hey all,
i have several delimited strings all of which have different amount of
elements is there a way to prevent the exception without a try catch block if
i try to check an element that doesn't exist?

thanks,
rodchar
 
hey all,
i have several delimited strings all of which have different amount of
elements is there a way to prevent the exception without a try catch block if
i try to check an element that doesn't exist?

thanks,
rodchar

Hi,

Of course, you can check if the length of the array is bigger than the
index you are trying to reach
 
hey all,
i have several delimited strings all of which have different amount of
elements is there a way to prevent the exception without a try catch block if
i try to check an element that doesn't exist?

thanks,
rodchar

sorry, fast keys :)

something like:
if ( elements.Length>4 )
fifthValue = elements[4];
 
tip:
(this work for me)

if you can pad your var with the delimiter char
if * is the delimeter and max expected array count = 100

then, you will always get 100 values (after split)

string s = "a*b*c..."; //string var
s += new string("*",100); //change 100 with the max number expected
string [] ss = s.Split('*');
 
thanks everyone for the help,
rod.

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

Of course, you can check if the length of the array is bigger than the
index you are trying to reach
 
Back
Top