Anuncios

Hay un post sobre este tema en español, haz click aqui.

Anuncios

Welcome to this post, today we’ll watch different ways to find the type of a variable.

Anuncios

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:

ValorConstantDescription
0vbEmptyVariable not initialized
1vbNullNull variable
2vbIntegerInteger de 2 bytes
3vbLongLong Integer de 4 bytes
4vbSingleSingle precision number
5vbDoubleDouble precision number
6vbCurrencyCurrency number
7vbDateDate
8vbStringString
9vbObjectObject
10vbErrorError number
11vbBooleanBoolean variable
12vbVariantVariant variable
13vbDataObjectData Object
14vbByteByte number
8192vbArrayArray
Anuncios
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.
Anuncios

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
isDateCheck if it’s a Date Type or can be modified
isEmptyCheck if it’s initializated or not
isNullCheck if it’s null or not
isNumericCheck if it’s numeric or not
isObjectCheck if it has a valid reference to an object
Anuncios
Note:
isNumeric is used most often.
Anuncios

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")
Anuncios

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:

Anuncios

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.

Anuncios

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:

Anuncios

Donación

Es para mantenimento del sitio, gracias!

$1.50