Link to home
Start Free TrialLog in
Avatar of quest_capital
quest_capital

asked on

An Array error with if statement.

I'm tring to split a string ie.(split[0],split[1]...etc) How do I code... If  (Split[1] == "" or null) do soming else?
Error: Index was outside the bounds of the array.

string body = ds.Tables[0].Rows[i]["body"].ToString();
                    string[] Split = body.Split('^');
                    if (Split[1] == "")//////////////////////// <<<<<<<<<<<< the error this line
                    {
                        lt.Text = body;
                    }
                    else
                    {
                        lt.Text = Split[1];
                    }
Avatar of Collindsouza
Collindsouza
Flag of United Kingdom of Great Britain and Northern Ireland image

quest_capital

String Body is empty..
so Split[1] is outside the bounds.. i.e Split[1] does'nt exist
Avatar of Dmitry G
I guess the string was not split, so array will have just 1 element only. So index 1 is out of bounds (it's index for 2nd element)

Use either index 0, or check that second element exists:

if(Split.Length>1 && Split[1] == "")
i tried setting your code as follows

            string body = "a^b^c^d^e";
            string[] Split = body.Split('^');
                if (Split[1] == "")//////////////////////// <<<<<<<<<<<< the error this line
                {
                    Console.WriteLine(body);
                }
                else
                {
                    Console.WriteLine(Split[1]);
                }
 
and it does work...

however when i set string body = "";

it gives me an Error: Index was outside the bounds of the array.

Yes, it seems it should be

if(Split.Length==1") // was not splitted
    lt.Text = body;
ASKER CERTIFIED SOLUTION
Avatar of Collindsouza
Collindsouza
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
2 Collindsouza

Your loop will never run if the string is not splitted, not once, because it checks that "i<0";

And obviously in the body of your loop you wanted Split[i], not Split[1]?

and I believe I showed the solution:

if(Split.Length==1") // was not splitted
    lt.Text = body;
else
   it.Text = Split[0] //// whatever value to set

Really, if the value should be set to the first element of array we don't need to run any chacks at all!
Just instead of if-else put one line only:


it.Text = Split[0]

That's enough, first element always exist.
why delete?? experts have provided a valid solution...
i suggest please split points between anarki_jimbel and Collindsouza
why delete?? experts have provided a valid solution...
i suggest please split points between anarki_jimbel and Collindsouza
I agree.
I agree too.
Well, I don't see any value to this question.  What's the point of checking Split[1] if Split.Length is 0?

Bob
This looks like a syntax error to me:
    if(Split.Length==1")

Bob
if(Split.Length==1")

of course it is :)

The question itself has no value (at least from my point of view).
However :
"
Comment from Collindsouza
Date: 01/11/2007 12:27AM PST
 Comment  

.... experts have provided a valid solution...  


I don't much care really :) ...