Difference between revisions of "Identifying data types"
From HashVB
m (Added VB6 header) |
|||
(One intermediate revision by one user not shown) | |||
Line 1: | Line 1: | ||
− | + | {{VB6}} | |
− | + | ||
− | + | ||
You can use the VB built in '''TypeName''' 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. | You can use the VB built in '''TypeName''' 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 this method. 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. | Please do note: It is a very healthy practice to actually not need this method. 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 | + | If you do not know about the multiple data types, please consult this page on [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. | Start a new project and paste the code below as Form1 code to illustrate the example. | ||
Line 17: | Line 13: | ||
Dim var3 As Byte 'declare a byte 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 TypeName(var1) 'Get the type for var1 | |
− | + | Form1.Print TypeName(var2) 'Get the type for var2 | |
− | + | Form1.Print TypeName(var3) 'Get the type for var3 | |
End Sub | End Sub |
Latest revision as of 13:48, 9 November 2005
This article is based on Visual Basic 6. Find other Visual Basic 6 articles. |
You can use the VB built in TypeName 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 this method. 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 page on 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 TypeName(var1) 'Get the type for var1 Form1.Print TypeName(var2) 'Get the type for var2 Form1.Print TypeName(var3) 'Get the type for var3 End Sub