For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic Syntax > March 2006 > detecting value types









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author detecting value types
feudalac

2006-03-21, 3:58 am

i have several textboxes. When 'ok' is pressed i need to findout if the
value is a string or an integer and then do what ever i need to do...

how do I find out value type?

i tryed:

dim a as variant
a = textbox1.text
if typeof a is integer then
... some code ...
end if

but type integer is not valid to be there... instead i am offered to
put some form names, ado and other stuff... but not value types


please help
Karl E. Peterson

2006-03-21, 10:09 pm

feudalac wrote:
> i have several textboxes. When 'ok' is pressed i need to findout if
> the value is a string or an integer and then do what ever i need to
> do...


The Textbox.Text property *always* returns a String. It's up to you how to
interpret it. You may want to use the Val() function, for instance, if you
feel it should be a number.
--
Working without a .NET?
http://classicvb.org/


Richard Mueller

2006-03-22, 7:08 pm


"Karl E. Peterson" <karl@mvps.org> wrote in message
news:eHcFdiSTGHA.5908@TK2MSFTNGP14.phx.gbl...
> feudalac wrote:
>
> The Textbox.Text property *always* returns a String. It's up to you how
> to
> interpret it. You may want to use the Val() function, for instance, if
> you
> feel it should be a number.
> --
> Working without a .NET?
> http://classicvb.org/
>
>


And, testing the text property to determine if the string value can be
interpreted as an integer can get tricky. You can use the IsNumeric function
to check if the string can be interpreted as numeric, but that does not mean
it is an integer. There is no IsInteger function. Unless someone has a
better idea, the best I can think of quickly to test if the value is integer
is:

Function IsInteger(strValue) As Boolean
Dim intValue As Integer
If (IsNumeric(strValue) = False) Then
IsInteger = False
Exit Function
End If
intValue = CInt(strValue)
If (CSng(intValue) = CSng(strValue)) Then
IsInteger = True
Else
IsInteger = False
End If
End Function

This assumes that if IsNumeric(strValue) returns True, then CInt(strValue)
and CSng(strValue) will never raise an error. I'm not sure if this is so.
Usually the best practice is just treat the value as a string, perhaps with
code similar to:

Select Case Textbox.Text
Case "1"
' do something.
Case "2"
' do something.
Case Else
' do something.
End Select

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net


Karl E. Peterson

2006-03-22, 7:08 pm

Richard Mueller wrote:
> Unless someone has a better idea, the best I can think of quickly to
> test if the value is integer is:
>
> Function IsInteger(strValue) As Boolean
> Dim intValue As Integer
> If (IsNumeric(strValue) = False) Then
> IsInteger = False
> Exit Function
> End If
> intValue = CInt(strValue)
> If (CSng(intValue) = CSng(strValue)) Then
> IsInteger = True
> Else
> IsInteger = False
> End If
> End Function
>
> This assumes that if IsNumeric(strValue) returns True, then
> CInt(strValue) and CSng(strValue) will never raise an error. I'm not
> sure if this is so.


Oh sure they will...

?csng(4E200)

> Usually the best practice is just treat the value
> as a string, perhaps with code similar to:
>
> Select Case Textbox.Text
> Case "1"
> ' do something.
> Case "2"
> ' do something.
> Case Else
> ' do something.
> End Select


That'd be one way. I usually just do something like this:

On Error Resume Next
MyInt = Val(Text1.Text)

If they enter garbage, MyInt retains whatever it was -- 0 if just
initialized.
--
Working without a .NET?
http://classicvb.org/


feudalac

2006-03-23, 8:08 am

Karl E. Peterson wrote:

> Richard Mueller wrote:
>
> Oh sure they will...
>
> ?csng(4E200)
>
>
> That'd be one way. I usually just do something like this:
>
> On Error Resume Next
> MyInt = Val(Text1.Text)
>
> If they enter garbage, MyInt retains whatever it was -- 0 if just
> initialized.



thanks all... i have an idea now, how to solve the problem!
Rick Rothstein

2006-03-23, 7:08 pm

> And, testing the text property to determine if the string value can be
> interpreted as an integer can get tricky. You can use the IsNumeric
> function to check if the string can be interpreted as numeric, but that
> does not mean it is an integer. There is no IsInteger function. Unless
> someone has a better idea, the best I can think of quickly to test if the
> value is integer is:
>
> Function IsInteger(strValue) As Boolean
> Dim intValue As Integer
> If (IsNumeric(strValue) = False) Then
> IsInteger = False
> Exit Function
> End If
> intValue = CInt(strValue)
> If (CSng(intValue) = CSng(strValue)) Then
> IsInteger = True
> Else
> IsInteger = False
> End If
> End Function


This is my standard posted function for a digit-only check...

Function IsDigitsOnly(Value As String) As Boolean
IsDigitsOnly = Len(Value) > 0 And Not Value Like "*[!0-9]*"
End Function

If the OP actually means he wants the restricted range of an Integer data
type then, after testing that it is digits only, I would drop the string
into a Currency data type and test if the number is within the range of an
Integer or not. If the OP is not restricting the number of characters that
may be typed in, then a test of the length of the input string would be a
first order of business.

Rick


Clinton

2006-03-24, 4:15 am


This should work (can be tweaked a bit). It will ignore Scientific notation
is desired:

Public Function Test1(strInput As String) As VbVarType
Dim ret As VbVarType

'Remove the > & "E0" < if scientific notation is considered numeric
If Not IsNumeric(strInput & "E0") Then
ret = vbString
Else
'Numeric
If CDec(strInput) - Int(strInput) = 0 Then
'Whole number.
'If this is all that you are after, then you can remove the rest
If CDbl(strInput) > -32769 And CDbl(strInput) < 32768 Then
ret = vbInteger
ElseIf CDbl(strInput) > -2147483649# And CDbl(strInput) <
2147483648# Then
ret = vbLong
Else
'Could be Single, or Double.
ret = vbDouble
End If
Else
'Could be Currency, Single, or Double.
ret = vbDouble
End If
End If

Test1 = ret
End Function

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com