Visual Basic 2008 9.0 .NET Examples and Ebook

Introduction to Visual Basic

Vorig Onderwerp

New in Visual Basic 2008 - 9.0

|

Arrays

Volgend Onderwerp

Variables

Vorig Onderwerp

Output and Comments in Modules

|

Some Operators

Volgend Onderwerp
What are Variables

What are Variables

Declaration of a Variable

Declaration of a Variable

Datatypes

Datatypes

Assignment

Assignment



What are Variables


This example will also bring the text "Hello World" to the console, but this time we will use variables.

Variables are dataholders which can be used at runtime to save information.
The content of the variable can change/vary at runtime.


Module Example
    Sub Main()
        Dim message As String                       ' declaration            (1)
        message = "Hello World !"                   ' initial assignment     (2)
        '
        ' or :
        ' Dim message As String = "Hello World !"   ' initialization         (3)
        '
        Console.WriteLine(message)
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 Hello World !

Klik hier om terug naar boven te gaan.  Up



Declaration of a Variable


Before our algorithm can use a variable, we will declare this variable to the compiler.

When declaring the variable we'll defined :
- the identifier of the variable
- the datatype of the variable, by using a "type specifier" in the form of an As-clause, keyword As followed by the name of the datatype

Keyword Dim is needed to mark this line as a declaration.


Klik hier om terug naar boven te gaan.  Up



Datatypes


Two often used datatypes are :
- String : used for alphanumeric data ( text )
- Integer : used for numeric data ( integral values )

The datatype specifies what information the variable can contain, and what actions we can perform on or with that variable. For instance, two numeric variables can contain numeric values which can be added, multiplied, divided, ... .


Klik hier om terug naar boven te gaan.  Up


Assignment


To set a variable to a specific value, the value has to be assigned to this variable. An assignation often looks like this : left-part = right-part

= is the assignation operator.

The value expressed by the right-part is assigned to the left-part.

The value ( to assign ) can be expressed is all kinds of forms. In the above example a String literal is used. But this could also be expressed by referring to another String variable.

It is also possible to initialize the variable in the declaration line.
Just put an assignation-clause ( = followed by the expression expressing the value to assign ) after the type specifier.

Between the parentheses of Console.WriteLine we place an expression that will evaluated to the value we want to output to the console.





This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.

Updated On : 2008-01-24

Download Broncode

Published On : 2008-06-24

Variables

Vorig Onderwerp

Output and Comments in Modules

|

Some Operators

Volgend Onderwerp

Introduction to Visual Basic

Vorig Onderwerp

New in Visual Basic 2008 - 9.0

|

Arrays

Volgend Onderwerp
Nederlands  Nederlands

Add to favorites (IE).