VB.NET from beginner to advanced programmer
- Introduction and Installation
- Objects and Events
- The basic data types
- Logical Operators
- Relational Operators
- If statements Then
- Arithmetical Operators
- Loops Part 1
- Loops Part 2
- Try Catch statements
- Subs and Functions
- Difference between Scopes
- Select Statements
- Multidimensional arrays
- Advanced Comments
- Compiling Directives
So now I will teach you about something called variables and constants, we'll start with variables.
Variables are places where you can store values and then at anytime go and check what value that is in there. We can compare it with a box where we can store some papers with information.
In VB.NET you need(like in most programming languages) declare the variable first. If we uses our box model, it's the same as creating the box. We can't put something in a box that doesn't exists.
This is the syntax for declaring a variable:
<scope> <variable_name> As [New] <type>
It is from where we can access the variable, this is more deeply covered in a later part.
It is the name of the variable, this should have the right prefix depending on variable type so you know what it is. For example if the type is integer we should add "n" at the beginning.
It's optional to add "New" here. If you do, the variable's value will be a new instance of the variable's type, or else the variable will just be empty until we give it a value. All types can't use the "New" option.
This is the type of the variable, this means what sort of values which can be stored in the variable, integer and date is an example of two different types.
Here comes an example:
Dim nInput as Integer
Dim is the local scope, it's used inside event block and sub/functions.
nInput is the name, the n is so we know it's an integer but it doesn't have any effect really. We could name a variable nInput even though it's not an integer but do not do that.
And at the end we make it the type Integer. This means we can store Integer values in it.
Look at this example:
Here we didn't declared what type it was. Then it automatically gets the type Object. The object type can store any value. Remember it's always better to set the variable type if you know what values that should be stored in it. If you have the type object it needs to be able to store everything and could therefor take more space even though it's empty.
The above code should actually got the prefix "o" since it's an object:
It's also possible to declare more then one variable at the time by using commas, you can set the type of all variables one by one or in groups:
One by one:
Dim a As Integer, b As Single, c As Doublea will be declared as an Integer, b as Single and c as double.
In a group:
Dim a, b, c As IntegerHere all three variables will be declared as Integers.
You can do a little like you want, as you can see in this example:
Dim a As Double, b, c As Integer, d, e, f As Date, ga will be a Double.
b and c will be Integers
d, e and f will be declared as Dates
and g will be an Object since we haven't wrote its type.
Set and get Values
When we want to store a value in the variable we write the variable name an equals to sign and the the value:
It could look like this:
In the above example the variable nRooms gets the value of 6.
It's also possible to assign a value to a variable when it's created, like this:
Dim nRooms as Integer = 6
To get the value from a variable we just simply writes its name where we want to access the value of it. Here's an example:
In the above example a messagebox will be showed with the value from the string variable called sMessage.
Don't get too confused when you got a variable on both sides of an equals sign:
REMEMBER: The variable is to the left and the value to the right. In this case the variable called nValue1 will get the same value as nValue2.
Constants are like variables but they can not change their value, you set it when you declare the constant. In our box model it would be like building a glass box without a hole around some papers, we cannot add or change what's in it but we can see everything through the glass.
Here's an example:
Const Pi as single = 3.14
See you in Part 4, then we will learn us about some basic data types.
Edited by Vswe, 21 March 2010 - 02:24 PM.