|
|
 |
|
|
|
|
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Hello World
| Following example will bring the text "Hello World" to the console. |
| Module Example
Sub Main()
Console.WriteLine("Hello World !")
Console.ReadLine()
End Sub
End Module Download Broncode |
Up
Comments
| Each line beginning with a single quote ( ' ) (3) is a comment line. These lines are ignored by the compiler, and will not alter the programs execution.
I would advise to use comment lines to clarify your program. This will help you later on when adaptations need to be made. Keep in mind, that when you make adaptations that you also update your comment lines. If you do not, your comment lines will lose their purpose. |
| Visual Studio : When default settings are used, comments are shown green. |
| ( abbreviation for remark ) is an alternative for the single quote : |
Up
Modules
| A module is a simple code block in which we write our sourcecode. Our sourcecode is written between Module (1) and End Module. |
| Visual Studio : To start a module in Visual Studio, you can click "Debug" and "Start Debugging". |
Up
Identifiers and Keywords
| Each module has a name, also called the identifier. For each module we can choose an identifier. Certain rules for identifier have to be followed, namely : - The name has to be one word, no spaces can be used. - The identifier has to be unique. For example each module in a certain project has to have a different name. - The following characters can be used to form the name: a to z, A to Z, 0 to 9 and an underscore ( _ ); although identifiers cannot start with a number. - The identifier may not start with a number. - The identifier cannot be a reserved keyword. These are already used in the language for specific constructions, for example Module (1), Sub (2) and End (6)(7) . As a consequence the compiler would not interpret these as identifiers , except when we put them between brackets, for example [Module] . In this case the compiler would recognize it as an identifier and not as a reserved keyword. |
| Visual Studio : When default settings are used for the editor, keywords are shown in blue. |
Up
Procedures
| The above module Example has a procedure with the identifier Main.
In a procedure we can write an algorithm to perform a certain routine.
A procedure starts with Sub (2) and ends with End Sub (6).
More than one procedure can be added to a module, but the execution of the program can only start with one procedure, this is always the Main procedure. |
Up
Output
| Console.WriteLine() will write the value expressed between the parentheses to the console. |
Up
Input
| After executing the code in the Main procedure the application is ended, and the console is closed. To avoid closing the console we can put a Console.ReadLine() at the end of the Main procedure. This will give us the opportunity to see what output we have on the console ( other uses for Console.ReadLine() will be explained later on ). Console.ReadLine() will read a line from the console. A line consists of zero or more characters ending with an endline character. An endline characters delimits a line. When the input is coming from the user, the endline is formed by pressing the "Enter" key. Only after the user presses "Enter" the program will continue with the instructions after Console.ReadLine(). When there are no following instructions ( like in the above example ), the program ( and console ) will close. |
Up
Strings and Literals
| The above example will always bring the text "Hello World !" to the console. This text is a constant, and will not vary at runtime. To express this text ( also called "string" ), we can use constant expressions, like "string literals", which are quoted within double quotes ( "..." ). |
| Visual Studio : When default settings are used, string literals are shown in red. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|