Solved

An Array error with if statement.

Posted on 2006-11-30
16
167 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

679 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