Variables is a key point of any programming language. But what is a variable exactly? Technically, it is a space reserved in memory that can also be changed.
First, a variable needs a type. Why? because the computer has to know what kind of variable it is. Think of a box where you can put a guitar, it will have a special shape for the guitar to fit into, but nothing else will be able to enter that box except for the guitar, it is sort of the same thing here.
Let’s say that a String type (that we have seen in the previous tutorial), has the shape of a square. And a Integer type(or Int if you prefer), has a round shape.
Integer = Non decimal numbers positive and negative, for example : -19 or 2893 or 829134
String = Multiple characters one after the other, for example : “Hello I am a string”
Let’s Look At Some Code
If I try to put a square into the round, it just won’t fit, same thing for the variables, you need to put the right product in the right container.
It is going to be done in two steps:
1. You have to tell the computer what will be the type of container it needs to reserve for you.
2. You have to put something into that container.
Here is what it looks like in code:
You can now see both steps but what exactly is “Dim”? Lets just say that it will tell the computer you want to begin variable declaration, or if you prefer: Hey computer, I am going to list the variable(s) I want you to reserve memory space for along with their type(s).
The WhatIsGoingToOutput is the name of the variable, simply a name that we will be using for ourselves to remember that in this container is something in particular, we could have named it anything, even A, B, Whatever.
Don’t forget that everything that starts with a ‘ is a comment, in other word, it’s a line that the computer won’t even consider when running the code, it’s strictly for us, humans.
Have you noticed that the text in the variable is now the same text that will be output in the console? Why not tell the computer to use the content of our variable for displaying it in the console directly. That way we can change the content that will be displayed by changing the variable.
You can change your code for:
Module Program Sub Main() 'Ask the computer for a new String type container Dim WhatIsGoingToOutput As String 'Put something into that container WhatIsGoingToOutput = "Hello all!" Console.WriteLine(WhatIsGoingToOutput) Console.ReadKey() End Sub End Module
We replaced the content in Console.WriteLine by WhatIsGoingToOutput variable. Try to Run the program by clicking Start.
There are different types of variables in Visual Basic (and C#). Microsoft has a great Web page to explain all that stuff into details. It even says how much memory they consume and what can be contained in each variable.
It’s pretty much the same thing for any types of variables, we will add another one to our code just for fun!
Module Program Sub Main() 'Ask the computer for a new String type container Dim WhatIsGoingToOutput As String Dim AnotherVariable As Integer 'Put something into that container WhatIsGoingToOutput = "Hello all!" AnotherVariable = 6 Console.WriteLine(WhatIsGoingToOutput) Console.WriteLine(6) Console.ReadKey() End Sub End Module
I followed the same steps we did with the String type variable, but now it’s an Integer type. You can Start the program again and the window below is what it should give you.
Hey! The Console.WriteLine() method can write Integer type and String type variables, you should keep that in mind for later!
One last thing to consider is where we can place variables in our code. Here, they are inside the Main() sub, we can place them elsewhere if we want. When we place a variable somewhere in particular, it will belong to a certain scope. The scope is in fact from where this variable will be accessible. We will go into more details as we progress. For now you know that you can place variables inside of Sub.
Question: would it work if I placed the WhatIsGoingToOutput = “Hello all!” line after the Console.WriteLine(WhatIsGoingToOutput)? The best thing is always to try it out to see the result. But in that case I can tell you, the instructions will be executed in order here, you need to put “Hello all!” in the variable before using it or else nothing will be written in the Console, and worst Visual Basic won’t like that, it will tell you that you need to assign your variable before using it! Always be careful with the order of things.
If you need more help, you can write a comment that I can answer or you can visit the page on variable declaration on the MSDN Website. This blog is not meant to replace the existing help from Microsoft that is, by the way, really well written, it is simply another approach to learning programming.