Hello,
I have a string:
MyString="1234".
I want to convert it to an integer or long.
MyLong=1234
Thx
Xmas
Printable View
Hello,
I have a string:
MyString="1234".
I want to convert it to an integer or long.
MyLong=1234
Thx
Xmas
VB Code:
Dim str As String = "1234" Dim inter As Integer inter = CType(str, Integer)
Very, very simple :(
Everything you wanted to know about type conversion functions:
ms-help://MS.VSCC/MS.MSDNVS/vblr7/html/vaGrpTypeConversion.htm
ms-help://MS.VSCC/MS.MSDNVS/vblr7/html/vafctctype.htm
No!
Not that simple!
Try this:
VB Code:
Dim str As String = "1234" Dim inter As Integer inter = Val(str)
The previous code generates error if your string contains anything besides nummbers. For example if str="1234DF"
Right Lunatic3 . The code I posted was based on his string value .
I thought val was no more to be used in VB.NET. What about
VB Code:
dim i as Integer dim s as string s = "1234" i = Integer.Parse(s)
You could have easily tested what you said. 'Val' is still present in .NET and i dont know if it is for backward compatibility or not, however Integer.Parse still produces error if the string contains anything beside numbers.Quote:
I thought val was no more to be used in VB.NET. What about
here's how to sort the integers out from the text in the string
Code:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String = "1234abc567xyz"
Dim excluded As String
Dim i As Integer
Dim l As Integer
For i = 0 To str.Length - 1
If IsNumeric(str.Chars(i)) Then
l = l & str.Chars(i)
Else
excluded = excluded & str.Chars(i)
End If
Next
MessageBox.Show("numbers:" & l & Chr(10) & "letters:" & excluded)
End Sub
Lunatic3
I didn't mean tp upset you. I was just wondering about the usage of Val() as it kind of doesn;t fit in with the new methods in .NET where most of the conversion methods are on objects Object.Method(). I would think its for backward compatibility only and Integer.Parse("2223") should be used instead. You can still put this in a Try block to catch erros.
Val() is math function . Parse() is string manipulation function .
I didnt get upset at all mate :)Quote:
I didn't mean tp upset you.
As you said 'val' does not fit into .NET way of thinking and it actually belongs to Microsoft.VisualBasic namespace. Still i found it easier and safer than trying to catch the error. Imagine you catch the error, then what you want to do with it? Are you going to apply a method to retrive the number out of that string? if so then you may use Val ;) .
val() is a math function LOCATED in the visualbasic namespace..so it needs vb backwards dll'sQuote:
Originally posted by Pirate
Val() is math function . Parse() is string manipulation function .
Still supported in VB.NET 2003 and I bet it will be for some ahead time .:DQuote:
Originally posted by PT Exorcist
val() is a math function LOCATED in the visualbasic namespace..so it needs vb backwards dll's