Hay un post sobre este tema en español, haz click aqui.
Welcome to this post, today we’ll watch different ways to find the type of a variable.
The first function is VarType, it takes a variable as argument and returns a numeric value to identify the type of variable. Let’s take a look to this table with the possible values:
| Valor | Constant | Description |
| 0 | vbEmpty | Variable not initialized |
| 1 | vbNull | Null variable |
| 2 | vbInteger | Integer de 2 bytes |
| 3 | vbLong | Long Integer de 4 bytes |
| 4 | vbSingle | Single precision number |
| 5 | vbDouble | Double precision number |
| 6 | vbCurrency | Currency number |
| 7 | vbDate | Date |
| 8 | vbString | String |
| 9 | vbObject | Object |
| 10 | vbError | Error number |
| 11 | vbBoolean | Boolean variable |
| 12 | vbVariant | Variant variable |
| 13 | vbDataObject | Data Object |
| 14 | vbByte | Byte number |
| 8192 | vbArray | Array |
Note:
In the case of Array, it gives back the value 8192 plus the type of array. For example, an integer array will be 8192 + 2 giving as final result 8194.
Another function is TypeName. This doesn’t return a numeric value instead returns a string with the name of type. Also we can find if the variable is from a determinate type with these functions:
| Funcion | Descripcion |
| isDate | Check if it’s a Date Type or can be modified |
| isEmpty | Check if it’s initializated or not |
| isNull | Check if it’s null or not |
| isNumeric | Check if it’s numeric or not |
| isObject | Check if it has a valid reference to an object |
Note:
isNumeric is used most often.
Let’s take a look on this example:
types.vbs
dim a
text = text & typename(a) & ": " & vartype(a) & chr(10)
a = null
text = text & typename(a) & ": " & vartype(a) & chr(10)
a = "Hello, world!"
text = text & typename(a) & ": " & vartype(a) & chr(10)
a = 100
text = text & typename(a) & ": " & vartype(a) & chr(10)
mens=MsgBox(text,64,"Different types")
First, we declare a variable named a with no value. Then we create a new variable named text where we store the value returned from typename and vartype on the variable a. Then, we’ll modify the value of a and text will store the new values from the functions. In all the cases, we’ll keep the previous values and we’ll separate with the chr(10). This is the character for a new line. When we finish, we use a msgbox to watch the final value of text. Let’s watch its output:

On this case, we watch how the first value is empty, no value, then returns null, the next one is string and the last is integer. In all cases returned the type name and the numeric type, just as we saw on previous tables.
In summary, today we watched how to find the data type of a variable, the available functions, the returned values, the functions to interrogate to variables, and one example to watch how these work. I hope you’ve found it useful. You can follow me on this social networks:


Donación
Es para mantenimento del sitio, gracias!
$1.50
