Welcome to the VB.NET tutorial series: "VB.NET from beginner to advanced programmer" which will take you from the very beginning to be a good programmer. VB.NET is a good first language for new programmers so this 21 part long series is written for completely beginners but it will also works perfectly fine if you already know another programming language.
VB.NET from beginner to advanced programmer
- Introduction and Installation
- Objects and Events
- Variables
- The basic data types
- Logical Operators
- Relational Operators
- If statements Then
- Arithmetical Operators
- Loops Part 1
- Arrays
- Loops Part 2
- Try Catch statements
- Subs and Functions
- Difference between Scopes
- Select Statements
- Multidimensional arrays
- Structures
- Classes
- Enumerations
- Advanced Comments
- Compiling Directives
So now I will teach you about something called variables and constants, we'll start with variables.
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.
Declaration:
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:
Code:<scope> <variable_name> As [New] <type>
<scope>
It is from where we can access the variable, this is more deeply covered in a later part.
<variable name>
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.
(New)
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.
<type>
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 is the local scope, it's used inside event block and sub/functions.Code:Dim nInput as Integer
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.Code:Dim nInput
The above code should actually got the prefix "o" since it's an object:
Code:Dim oInput
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:
a will be declared as an Integer, b as Single and c as double.Code:Dim a As Integer, b As Single, c As Double
In a group:
Here all three variables will be declared as Integers.Code:Dim a, b, c As Integer
You can do a little like you want, as you can see in this example:
a will be a Double.Code:Dim a As Double, b, c As Integer, d, e, f As Date, g
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:
Code:<variable_name>=<value>
It could look like this:
In the above example the variable nRooms gets the value of 6.Code:nRooms=6
It's also possible to assign a value to a variable when it's created, like this:
Code: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.Code:Messagebox.Show(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.Code:nValue1=nValue2
Constants
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:
Code:Const Pi as single = 3.14
See you in Part 4, then we will learn us about some basic data types.
Last edited by Vswe; 03-21-2010 at 03:24 PM.
Very nice. You may want to talk about how and where variables store information in memory (unless that is already in a later part). +rep
I don't think I've mentioned that. You'll probably find a few things missing, it's hard to remember to write about everything.![]()
I'd seen Dim a lot, but didn't know it defined a scope. Nice. +rep
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks