~ Programming in VB.Net


Let's take a look at this program:

1         Dim name As String
2         Dim age As Integer
3         name = "Peter"
4         age = 29
5         Console.WriteLine("Hello " & name & " you are " & age & " years old")
6         Console.WriteLine("This also means you are " & age * 12 & " months old")
7         Console.WriteLine("Bye " & name & "!")
8         Console.ReadLine()

you might be expecting it to print out:

Hello name you are age years old

But instead it says:

Hello Peter you are 29 years old
This also means you are 348 months old
Bye Peter!

What a friendly program! Let's break it down line by line:

  1. dim is a variable declaration, creating a temporary data store, a variable, and calling it name It also makes sure that whatever goes into name will be a string by setting it to as string
  2. We declare another variable called age and make sure it is stored as an integer (a whole number)
  3. The variable name that we created earlier is now assigned a value and as it's a string we better use speech marks - "Peter"
  4. The variable age that we created earlier is now assigned a value and as it's an integer we better not use speech marks - 29
  5. This line writes things to the screen, starting with the text "Hello " which attaches that variable we saw earlier to, but instead of putting the variable name, it puts the contents of the variable ("Hello Peter"), then it attaches some more text ("Hello Peter you are ") and adds another variable, age. Even though age is an integer we can stick it together with a string ("Hello Peter you are 29"). Then finally it uses the ampersand once more to attach the final piece of text ("Hello Peter you are 29 years old)
  6. This line works in pretty much the same way, but it does a calculation, working out the age in months. Computers are like giant calculators and you can perform all the sums you can perform on your little pocket calc performed and far far more using them!
  7. The great things about variables is that we can use them again and again, here we say "Bye " and using an ampersand stick on the name of the person. This is great, by using a variable we only need to write "Peter" once and save it as name. If someone else came along and wanted to change the program they just need to change the value of name. Programming is all about being as lazy as possible.
  8. Good old console.readline() stops the screen disappearing too fast
Variables work like labelled boxes that allow you to store things inside them to retrieve later.

What you have just seen is a declaration of two variables, name and age. A variable is a known or unknown value that has been given a symbolic name. This allows the name to be used independently of the value. It is advisable that a meaningful name for readability and convenience. This name is known as the identifier. To declare a variable in VB.NET we do the following:

Dim identifierName As datatype

Most programming languages have rules about identifiers: they generally have to use only Alphanumeric characters (a..Z0..9) and some languages are case sensitive (name != Name).

variable - short term memory used to store temporary values in programming code


Once you have declared a variable you need to assign it a value. Assignment, in programming terms, is the giving of a value to a variable, for example:

identifierName = 7

Here we are assigning the value 7 to the variable identifierName, so when we use identifierName, we mean the value 7:

1 dim identifierName as integer
2 identifierName = 7
3 console.writeline("The value stored in identifierName is: " & identifierName)


The value stored in identifierName is: 7


Source:https://en.wikibooks.org/wiki/A-level_Computing_2009 Creative Commons Licensed