|
-
May 11th, 2012, 03:18 AM
#1
Thread Starter
New Member
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.
-
May 11th, 2012, 04:08 AM
#2
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:
Dim myArray = myString.Split("."c)
That Split method is overloaded so it allows you to split in various ways.
-
May 11th, 2012, 02:02 PM
#3
Thread Starter
New Member
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.
-
May 11th, 2012, 10:14 PM
#4
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.
-
May 12th, 2012, 12:20 AM
#5
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|