How To Convert a VB.NET String To a Long

Hello Experts -

Does anyone know how to convert a string to a long in VB.NET?

This is the most confused language I've ever work with.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
If you KNOW the value in the String always has a VALID Long in it, then:
    Dim i As Long = CLng(yourStringVariableHere)

Open in new window

Otherwise, if you need to validate, then use:
        Dim i As Long
        If Long.TryParse(yourStringVariableHere, i) Then
            ' ... do something with "i" ...
            MessageBox.Show(yourStringVariableHere, "Invalid Long")
        End If

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
In addition to CLng() there is also Val().

Where as CLng requires the string to be purely numeric (i.e. a valid number), Val() doesn't blow up if the string contains non-numerics.

Val() skips leading white spaces, then begins converting the string into a number up until the point it encounters something that no longer looks like a number.  It returns a Double (so it works with floating point numbers as well).

Val("123ABC") = 123
Val("1.2.3") = 1.2 (2nd decimal point is no longer a valid number)
Val("ABC123") = 0
Val("1,234.56") = 1

Notice that last one... the comma doesn't look numeric to Val, so it stops conversion at the comma separator.  For situations like this, I've written my own subroutine  

The other thing I've done before is to write my own function if I knew the string might have embedded characters or spaces:
    Private Function StripToInt(ByVal Data As String)
        Dim NegativeSign As Boolean = False
        Dim Ch As Char
        For i = 1 To Len(Data)
            Ch = Mid$(Data, i, 1)
            If "0" <= Ch And Ch <= "9" Then
                StripToInt = StripToInt & Ch
            ElseIf Ch = "-" Then
                If Len(StripToInt) = 0 Then
                    NegativeSign = True
                End If
            End If
        If NegativeSign And Len(StripToInt) Then
            StripToInt = "-" & StripToInt
        End If
    End Function

Open in new window

The function simply reads through your string, and strips everything out that might not be a string.  You can then feed the results through CLng() (or integrate the CLng inside the function and have it return a Long rather than a string).
gbmcneilAuthor Commented:
This entire topic is far too complicated for what should be a very simple function.

Hats off to Microsoft for such a marvelous solution!

Rather than spend a week fooling with it, I am going to make the "converted to" value a double (i.e.,) -

LessrPrice = System.Convert.ToDouble(".21")

That way, I don't have to get involved with multiple lines of code.

Thanks to both of you for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.