2

I have a string "TextLine" that contains doubles and integers. Now I want to split the string into its parts and convert the resulting string array to double. Unfortunately I get an overload resolution error (for "parse") when I try to do that. What am I doing wrong?

Dim doubleAry As Double() = Array.ConvertAll(TextLine.Split(vbTab), [Double].Parse)

1 Answer 1

3

You can do it like this:

Dim doubleAry As Double() = Array.ConvertAll(TextLine.Split(vbTab), New Converter(Of String, Double)(AddressOf Double.Parse))

However, if the string array that you are giving it contains any invalid items, that will throw an exception and fail to convert any of the items. If you want to handle invalid items and just default them to 0, you could implement your own converter, like this:

Private Function DoubleConverter(ByVal text As String) As Double
    Dim value As Double = 0
    Double.TryParse(text, value)
    Return value
End Function

Then, you can use it like this:

Dim doubleAry As Double() = Array.ConvertAll(TextLine.Split(vbTab), New Converter(Of String, Double)(AddressOf DoubleConverter))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Steven..But when I run this line I get an FormatException error saying that the input string doesn't have the correct format. Any idea?
Then it probably doesn't :) If you want to handle format exceptions on each item individually, you would have to implement your own converter method. I'll update my answer with an example of that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.