|
-
Nov 25th, 2005, 08:09 AM
#1
Thread Starter
Lively Member
converting to integer
hi dear
can u tell me how can i chane a vribale containg some string to integer form.Actually i have a variable getting result from net and store it in string form,now i have to check if this variable contain a number then i have to change it's format to integer.else if it store some string for eg."program" then i've to set it to 0(zero).so if u know pls help me.
Thanks n regards.
Uttam
-
Nov 25th, 2005, 08:22 AM
#2
Re: converting to integer
You can use Val(Expresion). If it's a number it will return that number, else it returns 0 (cero)
VB Code:
Val("23") = 23
Val("HI23") = 0
-
Nov 25th, 2005, 08:25 AM
#3
Hyperactive Member
Re: converting to integer
Use IsNumeric
VB Code:
If IsNumeric(YourValue) Then
Debug.Print "I got your number bucko"
Else
MsgBox "Not a number jerk!"
End if
IsDate is another one you should squirrel away in the recesses of your brain for later use.
-
Nov 25th, 2005, 08:25 AM
#4
Fanatic Member
Re: converting to integer
VB Code:
Dim n As Integer
n = CInt(strValue)
you would obviously have to be sure there are only numeric values in that string
VB Code:
Private Function OnlyNumbers(ByVal s As String) As Boolean
Dim n As Integer
Dim bRes As Boolean
bRes = True
For n = 1 To Len(s)
If Not IsNumeric(Mid(s, n, 1)) Then
bRes = False
Exit For
End If
Next
OnlyNumbers = bRes
End Function
Edit: Too Late!!! Damn.
r0ach™
Don't forget to rate the post
-
Nov 25th, 2005, 08:29 AM
#5
Fanatic Member
Re: converting to integer
 Originally Posted by jcis
You can use Val(Expresion). If it's a number it will return that number, else it returns 0 (cero)
VB Code:
Val("23") = 23
Val("HI23") = 0
Ok, but:
r0ach™
Don't forget to rate the post
-
Nov 25th, 2005, 08:29 AM
#6
Hyperactive Member
Re: converting to integer
 Originally Posted by r0ach
Edit: Too Late!!! Damn. 
You got too fancy
-
Nov 25th, 2005, 09:14 AM
#7
Fanatic Member
Re: converting to integer
Not only would you need to insure the string contains ONLY numerals, you also need to make sure it contains no commas AND that IF it only contains numerals, that the number can be a VALID integer (zero to 32,767).
The following project I did to test this had one text box to enter your string, one to display whether the string can be converted to an integer and a button to kick off that analysis/conversion.
VB Code:
Private Sub Command1_Click()
Dim i As Integer
Dim my_int As Integer
'Get rid of any commas in your string
Text1.Text = Replace(Text1.Text, ",", "")
'Since largest integer is 32767 quick exit if string is longer
'than 5 characters
If Len(Text1.Text) > 5 Then
Text2.Text = "This string can NOT be converted to an integer."
Exit Sub
End If
' Check each character in string to see if it's a number, exit if not
' Ascii value must be 48 to 57 to be a numeral
For i = 1 To Len(Text1.Text)
If Asc(Mid(Text1.Text, i, 1)) < 48 Or Asc(Mid(Text1.Text, i, 1)) > 57 Then
Text2.Text = "This string can NOT be converted to an integer."
Exit Sub
End If
Next i
' Check value of numeral to see if is 32767 or less
If Val(Text1.Text) >= 0 And Val(Text1.Text) <= 32767 Then
Text2.Text = "This string CAN be converted to an integer."
my_int = CInt(Text1.Text)
MsgBox "String has been converted to an integer.", vbOKOnly, "Integer Conversion"
Exit Sub
Else
Text2.Text = "This string can NOT be converted to an integer."
End If
End Sub
Do canibals not eat clowns because they taste funny? 
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
|