Basics
In this part of the Visual Basic tutorial, we will cover basic programming concepts of the Visual Basic language. We introduce the very basic programs. We will work with variables, constants and basic data types. We will read and write to the console; we will mention variable interpolation.The following is a simple Visual Basic program.
Option Strict OnWe will explain the program line by line.
Module Example
Sub Main()
Console.WriteLine("This is Visual Basic")
End Sub
End Module
Option Strict On
Option Strict
statement can be either On or Off. The default is Off, for backward compatibility with Older Visual Basic programs. This statement is recommended in all your new programs. When the statement is On, the compiler will detect various bad programming practices. Module ExampleEach Visual Basic program should be properly structured. The source code is placed into modules. Within modules, the code is further divided into procedures and functions. A properly structured module is easier to maintain and is less error prone. In our case, we create a module called Example. For this, we use the
...
End Module
Module
keyword. A module definition is ended with the End Module
statement. Sub Main()With a
...
End Sub
Sub
keyword, we create a procedure. Sub is derived from the subroutine word. Subroutines and procedures are synonyms. In Visual Basic, the preferred word is the procedure. A procedure is a block of Visual Basic code. This is to achieve modularity of the code. Console.WriteLine("This is Visual Basic")In this code line, we print the "This is Visual Basic" string literal to the console. To print a message to the console, we use the
WriteLine()
method of the Console
class. It represents the standard input, output, and error streams for console applications. $ ./simple.exeExecuting the program gives the above output.
This is Visual Basic
We can use a Console class to read values as well.
Option Strict OnThe second program will read a value from a console and print it.
Module Example
Dim name As String
Sub Main()
Console.WriteLine("Enter your name: ")
name = Console.ReadLine()
Console.WriteLine("Hello {0}", name)
End Sub
End Module
Dim name As StringThe
Dim
keyword is used to declare a variable. The variable is called 'name'. Unlike constants, which store only one value during the life of the program, variables may store various different values. The As
keyword defines the data type of the variable. Our variable will hold string values. name = Console.ReadLine()We read a line from the terminal. When we hit the Enter, the string is assigned to the name variable.
Console.WriteLine("Hello {0}", name)In this code line, we perform variable interpolation. Variable interpolationis replacing variables with their values inside string literals. Another names for variable interpolation are: variable substitution and variable expansion.
$ ./readline.exeThis is the output of the second program.
Enter your name:
Jan
Hello Jan
Command line arguments
Visual Basic programs can receive command line arguments. There are several ways to retrieve arguments from the command line.Option Strict OnIn the above program, we get the command line arguments and print them to the terminal.
Module Example
Dim cline As String
Sub Main()
cline = Command()
Console.WriteLine(cline)
End Sub
End Module
cline = Command()The
Command()
function is used to get the arguments. Console.WriteLine(cline)We print the command line arguments to the terminal.
$ ./commandargs.exe 1 2 3We execute the program followed by three numbers. The program prints the numbers as well as the program name. Program name is the first command line argument.
/home/vronskij/programming/basic/basics/commandargs.exe 1 2 3
In Visual Basic 2008 Express Edition, select project properties. In the Debug tab, there is a text area for specifying the command line arguments.

Figure: Command line arguments
Option Strict OnCommand line arguments can be passed to the Main procedure.
Module Example
Dim size As Integer
Sub Main(ByVal cmdArgs() As String)
size = cmdArgs.Length
If size > 0 Then
For i As Integer = 0 To size - 1
Console.WriteLine(cmdArgs(i))
Next
End If
End Sub
End Module
Sub Main(ByVal cmdArgs() As String)This Main() procedure receives a string array of command line arguments.
size = cmdArgs.LengthWe determine the size of the array.
If size > 0 ThenWe go through the array and print all arguments to the console. Note that in this case, the name of the program is not included among the arguments.
For i As Byte = 0 To size - 1
Console.WriteLine(cmdArgs(i))
Next
End If
$ ./cmdargs.exe 2 3 5We provide three numbers as command line arguments and these are printed to the console.
2
3
5
Variables and Constants
A variable is a place to store data. A variable has a name and a data type. A data type determines, what values can be assigned to the variable. Integers, strings, boolean values etc. Over the time of the program, variables can obtain various values of the same data type. Variables are always initialized to the default value of their type before any reference to the variable can be made. Variables are declared with theDim
keyword. Unlike variables, constants retain their values. Once initialized, they cannot be modified. Constants are created with the Const
keyword. Option Strict OnIn the above example, we work with four variables.
Module Example
Sub Main()
Dim city As String = "New York"
Dim name As String = "Paul", age As Integer = 35, _
nationality As String = "American"
Console.WriteLine(city)
Console.WriteLine(name)
Console.WriteLine(age)
Console.WriteLine(nationality)
city = "London"
Console.WriteLine(city)
End Sub
End Module
Dim city As String = "New York"We declare a city variable of the String type and initialize it to the "New York" value.
Dim name As String = "Paul", age As Integer = 35, _We can declare and initialize more variables with one
nationality As String = "American"
Dim
keyword; they are separated with a comma character. Console.WriteLine(city)We print the values of the variables to the terminal.
Console.WriteLine(name)
Console.WriteLine(age)
Console.WriteLine(nationality)
city = "London"We assign a new value to the city variable.
As we already said above, constants cannot change their initial values.
Option Strict OnIn this example, we declare two constants and one variable.
Module Example
Sub Main()
Const WIDTH As Integer = 100
Const HEIGHT As Integer = 150
Dim var As Integer = 40
var = 50
Rem WIDTH = 110
End Sub
End Module
Const WIDTH As Integer = 100We use the
Const HEIGHT As Integer = 150
Const
keyword to inform the compiler, that we declare a constant. It is a convention to write constants in upper case letters. Dim var As Integer = 40We declare and initialize a variable. Later, we assign a new value to the variable.
var = 50
Rem WIDTH = 110This is not possible with a constant. If we uncomment this line, we will get a compilation error.
Variable interpolation
Variable interpolation is replacing variables with their values inside string literals. Another names for variable interpolation are: variable substitution and variable expansion.Option Strict OnIn Visual Basic, strings are immutable. We cannot modify an existing string. Variable interpolation happens during string creation.
Module Example
Dim age As Byte = 34
Dim name As String = "William"
Dim output As String
Sub Main()
output = String.Format("{0} is {1} years old.", _
name, age)
Console.WriteLine(output)
End Sub
End Module
Dim age As Byte = 34Here we declare three variables.
Dim name As String = "William"
Dim output As String
output = String.Format("{0} is {1} years old.", _We use the
name, age)
Format()
method of the built-in String module. The {0}, {1} are the places, where the variables are evaluated. The numbers represent the position of the variable. {0} evaluates to the first supplied variable, {1} to the second etc. $ ./interpolation.exeOutput.
William is 34 years old.
This chapter covered some basics of the Visual Basic language.
0 comments:
Post a Comment