Solved

An Array error with if statement.

Posted on 2006-11-30
16
168 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
array not updating 8 42
Load XML element 3 43
.NET Redemption/Outlook Examples 3 32
Trying to check to see if email is in valid format in asp.net 3 21
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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