Difference between revisions of "Is it an integer"

From HashVB
Jump to: navigation, search
(Added an alternate method to ..Code samples/Variables/Is it an integer)
Line 8: Line 8:
  
 
Simple as that.
 
Simple as that.
 
 
----
 
 
 
Alternative
 
 
 
You can use the '''getVarType''' function described below to obtain more exact information on a variable type. This function returns the type of a variable. Doesn't work for all possible declarations to a variable, but with all the common ones.
 
 
Please do note: It is a very healthy practice to actually not need any of the methods described on this page. Carefully plan and organize your code to hold the correct variable type for the specific purpose. Consider the limits that the variable may need to step over at a certain time, and plan ahead.
 
 
If you do not know about the multiple data types, please consult this link:
 
[[http://www.officecomputertraining.com/vbtutorial/tutpages/page12.asp Data types]]
 
 
 
Start a new project and paste the code below as Form1 code to illustrate the example.
 
 
Private Sub Form_Load()
 
Dim var1 As Boolean    'declare a boolean variable
 
Dim var2 As Integer    'declare an integer variable
 
Dim var3 As Byte        'declare a byte variable
 
 
Form1.AutoRedraw = True    'setting this to true just so that .print will be visible
 
Form1.Print getVarType(var1)    'Get the type for var1
 
Form1.Print getVarType(var2)    'Get the type for var2
 
Form1.Print getVarType(var3)    'Get the type for var3
 
End Sub
 
 
'Function that returns variable type
 
Public Function getVarType(ByVal variable) As String
 
    getVarType = TypeName(variable)
 
End Function
 

Revision as of 19:56, 16 September 2005

So, you want to tell if a number is an integer?

VB has a very useful function called Int() that will give you the integer portion of a number. You can then compare this to the original, and if they match, it is an integer.

Function IsInteger(byval Value) as Boolean
  IsInteger = (Int(Value) = Value)
End Function

Simple as that.