Results 1 to 5 of 5

Thread: String[] [RESOLVED]

  1. #1

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    String[] [RESOLVED]

    Howdy all,


    I am having a problem accessing the values of a string[] array;

    Code:
    //remove the leading spaces
    					line = line.TrimStart(new char[] {' '});
    					//Console.WriteLine(line);
    					//find the parts to the line..
    					line2 = line.Split(new char[] { ',' });
    					//we now have, the parts to the line..
    					left = line2[0].Split(new char[] { ' ' });
    					//get the right side..
    					right = line2[1];
    It generates an Index was outside the bounds of an array error on the line:

    Code:
    right = line2[1];
    Anyone know why?


    Phreak
    Last edited by «°°phReAk°°»; Feb 12th, 2004 at 12:33 AM.

    Visual Studio 6, Visual Studio.NET 2005, MASM

  2. #2
    Lively Member
    Join Date
    Oct 2003
    Location
    Guildford, UK
    Posts
    91
    The only way that can happen is if line2 doesn't contain any ',' characters, so when you try and split it the CLR returns a one element array which of course doesn't have a [1] index.

  3. #3

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    See, I know the basics of it. But if that was the problem, how come I can do it this way (im sure there is a more efficient way):

    Code:
    for (int i = 0; i < line2.GetUpperBound(0)+1; i++)
    {
        right = line2[i];
    }
    
    //now right = what it should
    
    //but this doesnt work
    right = line2[1];

    Is this weird?


    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  4. #4
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    No that isn't wierd. At first, you check and incriment until you hit the last element in the array, then you access an element NOT in the array. The loop you made obviously only gets run once (since the array's upper bound is [0], thus [1] doesn't work).

    Your code that you use to split a file into a string is either wrong, or you didn't set the string array to a high enough number you need (array bounds must be declared at compile time)

  5. #5

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    See, I know what you mean. But if I have:

    "Mondays Event,soccer"

    line2[0] = "Mondays Event"

    and line2[1] should = "soccer"

    It returns an error when i use line2[1]

    But, when i use that loop, and:

    Code:
    right = line2[i]
    (Notice i used UpperBound(0)+1)

    line2[1] equals "soccer" just like i want it to.


    I will try when I am home to set the array to a fixed size. But I have seen them used before like I have.


    [edit]
    PS: I have it working how I want now. Just wondering why my first attempt didn't work, but the loop does.
    [/edit]

    Phreak
    Last edited by «°°phReAk°°»; Feb 12th, 2004 at 12:28 AM.

    Visual Studio 6, Visual Studio.NET 2005, MASM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width