Solved

An Array error with if statement.

Posted on 2006-11-30
16
165 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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