- Defining a variable is the first step for Visual Basic programming. In any programming language, defining variables gives the developer a method of saving calculation results and input from the user. Below is an example of how to define a string variable:
Dim strMyVariable as String
The "dim" statement is short for "dimension." The term is used in defining any variable in the code file. Standards for development place defined variables at the top of the code file. The "String" term tells the compiler that the variable will be used to hold characters. The following code assigns a string value to the strMyVariable:
strMyVariable = "My First App."
It's important to assign the right type of entries to a variable. For instance, the code below would generate an error. The code tries to assign a number to a string variable.
strMyVariable = 0 - It's standard in the development industry to use the string "Hello World" for coding practice. The following code is an example of a basic program that displays "Hello World" to the user console.
Public Module myFirstProgram
Sub Main()
Dim strHello as String
strHello = "Hello World"
Console.WriteLine (strHello)
End Sub
End Module
The "module" statement encapsulates the code. This is required in every Visual Basic class files. The "Main()" statement is the entry point for the application. When a user starts a Visual Basic application, the compiler looks for "Main" as the beginning of the program. The meat of the application is within the Main function. First, the "strHello" is defined, which tells the compiler that memory resources are needed for a string. The next statement assigns a string of characters to strHello. Finally, the "WriteLine" functions is used to display the string. "Console.WriteLine" is a class and function included with the Visual Basic library, which is why this variable does not need to be declared. Microsoft includes hundreds of pre-packaged libraries with the company's compiler.
previous post
next post