Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

VB.NET from beginner to advanced programmer Part 3 - Variables

VB.NET form variable type

  • Please log in to reply
3 replies to this topic

#1 Vswe


    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 01 November 2009 - 05:18 PM

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

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.

<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.

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:

Dim nInput

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:

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:
Dim a As Integer, b As Single, c As Double
a will be declared as an Integer, b as Single and c as double.

In a group:
Dim a, b, c As Integer
Here 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, g
a 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.

  • 3

#2 Guest_Jordan_*

  • Guest

Posted 02 November 2009 - 05:30 AM

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
  • 0

#3 Vswe


    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 02 November 2009 - 05:32 AM

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. :)
  • 0

#4 WingedPanther73


    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 02 November 2009 - 05:18 PM

I'd seen Dim a lot, but didn't know it defined a scope. Nice. +rep
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download