|
-
Apr 18th, 2003, 12:35 PM
#1
Thread Starter
Hyperactive Member
String to byte (not array)... Quite simple (I think)
Hello,
I have a string:
MyString="1234".
I want to convert it to an integer or long.
MyLong=1234
Thx
Xmas
Learn, this is the Keyword...
-
Apr 18th, 2003, 12:42 PM
#2
Sleep mode
Re: String to byte (not array)... Quite simple (I think)
VB Code:
Dim str As String = "1234"
Dim inter As Integer
inter = CType(str, Integer)
-
Apr 18th, 2003, 12:44 PM
#3
Thread Starter
Hyperactive Member
Very, very simple
Learn, this is the Keyword...
-
Apr 18th, 2003, 01:08 PM
#4
Fanatic Member
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
-
Apr 18th, 2003, 01:11 PM
#5
Frenzied Member
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"
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Apr 18th, 2003, 01:25 PM
#6
Sleep mode
Right Lunatic3 . The code I posted was based on his string value .
-
Jun 12th, 2003, 12:54 PM
#7
Fanatic Member
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)
Using VB.NET 2003/.NET 1.1/C# 2.0
http://del.icio.us/rajoo
Blow your mind, smoke gunpowder
Ashes to ashes, dust to dust
If God won't have you, the devil will. - Author unknown
Don't follow me, I'm lost too ...
-
Jun 12th, 2003, 02:38 PM
#8
Frenzied Member
I thought val was no more to be used in VB.NET. What about
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.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Jun 12th, 2003, 02:44 PM
#9
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
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Jun 12th, 2003, 03:05 PM
#10
Fanatic Member
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.
Using VB.NET 2003/.NET 1.1/C# 2.0
http://del.icio.us/rajoo
Blow your mind, smoke gunpowder
Ashes to ashes, dust to dust
If God won't have you, the devil will. - Author unknown
Don't follow me, I'm lost too ...
-
Jun 12th, 2003, 03:14 PM
#11
Sleep mode
Val() is math function . Parse() is string manipulation function .
-
Jun 12th, 2003, 03:30 PM
#12
Frenzied Member
I didn't mean tp upset you.
I didnt get upset at all mate 
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 .
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Jun 12th, 2003, 07:31 PM
#13
yay gay
Originally posted by Pirate
Val() is math function . Parse() is string manipulation function .
val() is a math function LOCATED in the visualbasic namespace..so it needs vb backwards dll's
\m/  \m/
-
Jun 13th, 2003, 06:56 AM
#14
Sleep mode
Originally posted by PT Exorcist
val() is a math function LOCATED in the visualbasic namespace..so it needs vb backwards dll's
Still supported in VB.NET 2003 and I bet it will be for some ahead time .
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
|