Anuncios

Existe una version en Español de este post, haz click aqui.

Welcome to this post, today we’ll talk about a classic theme of every language as are the variables because they allow us store information to our codes.

Anuncios

VBscript defines a variable like Basic o Visual Basic. You can define it directly with the name o through dim word. Both cases are valid but dim is more useful to have a better organization of our variables to debug an error, its syntax is:

dim var_name
Anuncios

Let’s see a few examples:

dim name
name = "tinchicus"
site = "tinchicus.com"
Anuncios

Both variables are valid, the first one is declared and then defined. Instead, site is directly defined and can be used but at the moment of the code debugging and it’ll difficult our search.

Anuncios

We can use letters, numbers and special characters to make the name of variables. We can use any word that not match with any reserved word, in example msgbox, inputbox, for, dim, etc. The variable names must begin with letters not with numbers or special characters (except underscore), let’s see some examples:

valid names
name, Name, name43, n4m3, _name
not valid names
43name, #name, -name, $na#me, año
Anuncios

While more data types are available for variables we can manage in a better way information. Through this we can reserve adecuate space in memory and optimize its use but this complicate our language. One solution that gived us Visual Basic was the data type named Variant. Its main feature is manage any data type in a generic way. Sounds great but what is the loss? It loses all the above features but still is a great type and it’s used by other languages.

Anuncios

With this type and dim we don’t need to worry about how to declare a type of variable, making more easy to manage it. Let’s see some examples, first create a file with this code:

var.vbs

dim var
var = 5
msgbox var,,"Variables"
var = "Hola, Mundo!"
msgbox var,,"Variables"
Anuncios

We declara a variable named var then we define a value for this variable. The next line call to msgbox to show value of var. Now we change the value to another data type and we show this new value. Let’s try this code:

Anuncios

In this case we have the first value, let’s see the next msgbox

Anuncios

Now we can see the new value. One detail, the variable change its type automatically only with the new value. This is one advantage from Variant type. Let’s take this code and make one change:

dim var1, var2
var1 = 5
VAR2 = 10
total = var1 + Var2
msgbox total,,"Variables"
Anuncios

We’ll see some particularities. The first is the way of more one variable declaration, you can add all you need on this way, and then we define two variables with the same name but the second with uppercase. The next variable is the sum of the two variables and finally show the value of the final variable. Let’s see its output:

Anuncios

It works because VBscript is not case sensitive but this can be dangerous. We can use two variables with same name but with upper and lower cases and this can change the value when it must not. Although we saw a direct definition for a new variable but this is not a good practice if we must make a debug. Let’s see a few tips for variables:

  • They’re declared dynamicly
  • We can use dim but it is not mandatory
  • All variables are Variant
  • We can use letters, numbers or underscore for the name
  • They can’t start with numbers or special characters
  • VBscript is not case sensitive, be careful to declare names, always must be differents
  • The types can change dynamicly
Anuncios

In summary, we saw variables, what they are, how to use it, some features of this language, some examples to saw its different behaviours, I hope you’ve found it useful. You can follow me on this social networks:

Anuncios

Donation

It’s for maintenance of the site, thanks!

$1.50