|
|
 |
|
|
|
|
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. |
| 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 followed : - One word, no spaces can be used. - The identifier has to be unique. For example each module in a certain projects has to have a different name. - The characters a to z, A to Z, 0 to 9 and an underscore ( _ ) can be used. - The identifier may not start with a number. - The identifier can not be a reserved keyword. These are used in the language for specific constructions. The compiler wouldn't interpret these as identifiers, except when we put them between brackets. For example, 'Module' (1), 'Sub' (2) and 'End' (6)(7) are reserved words, which can not be used as an identifier. But '[Module]' would be understood by the compiler as an identifier. |
| 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 a algorithm to perform a certain routine.
A procedure starts with Sub (2) and ends with End Sub (6).
More than one procedures 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. Console.ReadLine() will read a line from the console. A line consists of zero or more characters ended with an endline character. 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 constant, and will not vary at runtime. To express this text ( also called "string" ), we can use constant expressions, like "string literals", which are surrounded with double quotes ( "..." ). |
| Visual Studio : When default settings are used, string literals are shown in red. |
This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|