Results 1 to 5 of 5

Thread: String Split Headache

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    3

    String Split Headache

    I'm trying to split a string at a period character and save the two halves. I want to later evaluate the length of these, but I can't get the Split function to work at all.

    Code:
    Dim myArray() As String = Split(myString, ".", , CompareMethod.Text)
    I thought that this would split the two halves into an array as the help documentation said it would, but I just get the "A first chance exception of type 'System.IndexOutOfRangeException' occurred" error. This is really bugging me, as I can't even do this as the documentation tells me to do it. I try to run their example and the same thing happens.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: String Split Headache

    Don't use that Split function. It's a hangover from VB6. Use the .NET-specific Split method, which is a member of the String object itself:
    vb.net Code:
    1. Dim myArray = myString.Split("."c)
    That Split method is overloaded so it allows you to split in various ways.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    3

    Re: String Split Headache

    I did a little more coding and came up with how I wanted to use this, but it throws a bunch of exceptions at me, even though the code works fine.

    Code:
     totalPriceString = totalPrice.ToString
                Dim priceArray() = totalPriceString.Split("."c)
    
                If ((priceArray IsNot Nothing) And (UBound(priceArray) = 0)) Then ' I have no decimals
                    totalPriceString = totalPrice.ToString & ".00"
                    salesTaxString = salesTax.ToString & ".00"
                ElseIf ((priceArray IsNot Nothing) And (UBound(priceArray) = 1)) Then ' I have one or two decimals already
                    If (priceArray(1).Length = 1) Then
                        totalPriceString = totalPrice.ToString & "0"
                        salesTaxString = salesTax.ToString & "0"
                    End If
                End If
    The exceptions:

    Code:
    A first chance exception of type 'System.IndexOutOfRangeException' occurred
    A first chance exception of type 'System.NullReferenceException' occurred
    A first chance exception of type 'System.NullReferenceException' occurred
    A first chance exception of type 'System.NullReferenceException' occurred
    A first chance exception of type 'System.NullReferenceException' occurred
    A first chance exception of type 'System.NullReferenceException' occurred
    A first chance exception of type 'System.IndexOutOfRangeException' occurred
    A first chance exception of type 'System.IndexOutOfRangeException' occurred
    A first chance exception of type 'System.IndexOutOfRangeException' occurred
    A first chance exception of type 'System.IndexOutOfRangeException' occurred
    I understand what the exceptions mean, just not why I am getting them in this case when I specifically checked to prevent them...
    Last edited by Rajada; May 11th, 2012 at 05:47 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: String Split Headache

    If all you're getting is a message that an exception was thrown then that means that the exception is thrown and caught in Framework code, so it's not your issue. You aren't actually catching any of these exceptions yourself are you? You might also try extending that code out so that every line does just one thing and then stepping through it line by line. You can then determine exactly which line each exception is caused by.

    Also, that UBound function is also a VB6 holdover. You should use the GetUpperBound method of the array itself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    3

    Re: String Split Headache

    Wow, why is the documentation telling me to use all these VB6 functions? Once I used GetUpperBound all my errors went away. Thanks a bunch man.

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