index was outside the bounds

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
 
I

Ignacio Machin ( .NET/ C# MVP )

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
 
I

Ignacio Machin ( .NET/ C# MVP )

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];
 
R

raulavi

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('*');
 
R

rodchar

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top