Solved

An Array error with if statement.

Posted on 2006-11-30
16
163 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
  • 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 29

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
 
LVL 29

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 29

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 29

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 29

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now