Solved

An Array error with if statement.

Posted on 2006-11-30
16
169 Views
Last Modified: 2010-04-16
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];
                    }
0
Comment
Question by:quest_capital
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
  • +1
16 Comments
 
LVL 5

Expert Comment

by:Collindsouza
ID: 18050522
quest_capital

String Body is empty..
so Split[1] is outside the bounds.. i.e Split[1] does'nt exist
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 18050530
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] == "")
0
 
LVL 5

Expert Comment

by:Collindsouza
ID: 18050555
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.

0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 18050556
Yes, it seems it should be

if(Split.Length==1") // was not splitted
    lt.Text = body;
0
 
LVL 5

Accepted Solution

by:
Collindsouza earned 250 total points
ID: 18050567
you can put it in a loop as follows

            string body = ds.Tables[0].Rows[i]["body"].ToString();
            string[] Split = body.Split('^');
            for (int i=0; i < Split.Length - 1; i++)
            {
                if (Split[1] == "")
                {
                    Console.WriteLine(body);
                }
                else
                {
                    Console.WriteLine(Split[1]);
                    break; // condition met exit loop
                }
            }
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 18051027
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.
0
 
LVL 5

Expert Comment

by:Collindsouza
ID: 18290914
why delete?? experts have provided a valid solution...
i suggest please split points between anarki_jimbel and Collindsouza
0
 
LVL 5

Expert Comment

by:Collindsouza
ID: 18365154
why delete?? experts have provided a valid solution...
i suggest please split points between anarki_jimbel and Collindsouza
0
 
LVL 7

Expert Comment

by:icr
ID: 18368612
I agree.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 18380648
I agree too.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18432940
Well, I don't see any value to this question.  What's the point of checking Split[1] if Split.Length is 0?

Bob
0
 
LVL 96

Expert Comment

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

Bob
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 18440706
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 :) ...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question