Getting Started with Basic Programming
The following sections introduce the basic constructs of the Visual Basic .NET language.
Understanding Variables
A developer won’t get too far in writing a program without using variables. The best way to think of a variable is as a little mailbox slot in the computer. The program can put something (a value) in the mailbox or look at the contents therein.Variables also have a type, meaning that the mailbox can hold only a certain kind of value. An integer variable, for example, can hold numeric whole values, and a string variable can hold a sequence of characters. Table A-1 describes commonly used variable types. This list is by no means exhaustive—it’s meant only to illustrate that you can use different variables in different ways.
NAME | DESCRIPTION |
---|---|
Integer | Whole numbers |
Boolean | Either special value True or special value False |
String | A sequence of characters |
Single | A floating-point number such as 1.41 or 3.3333333 |
When a variable is required in a program, it first has to be declared.Declaring avariable involves giving it a name and defining the data type that it’ll hold:
Dim iNumber as Integer
The previous line of code declares a variable named iNumber and specifies that the variable will hold integer, or whole number, values.
Note | The keyword Dim, used when declaring variables, is a holdover from old versions of Visual Basic. It’s short for Dimension. |
Placing a value into a variable is called assigning a value to the variable. You do this using the equals sign:
iNumber = 41
After the previous line of code runs, the iNumber mailbox will hold the value 41.
Understanding Operators
An operator is something that performs an action on one or more things. They’re also grouped into separate categories. For example, the set of arithmetic operators perform mathematical functions. Even the nonprogrammer should recognize these examples of the plus and minus operators:
Dim x As Integer
Dim y As Integer
Dim r As Integer
x = 41
y = 35
r = x + y
Debug.WriteLine("answer is " & r)
r = x - y
Debug.WriteLine("answer is " & r)
To summarize what this program does, the first three integer variables (named x, y, and r) are declared. Then, the variables x and y are assigned values. The next line of code adds the values of the variables x and y together and stores the result in the variable r. The next line of code (the one that starts Debug.Writeline...) outputs the answer to a special window in Visual Studio called the Debug window. Finally, the value in the variable y is subtracted from the value in the variable x, and the result is placed in the variable r, which is output to the Debug window.
Note | The second assignment of the variable r in the previous code will overwrite whatever was held in the variable before. Consider variables as mailboxes that can hold only a single thing. Placing a second thing in the mailbox pushes whatever used to be there out the back of the mailbox and into the trash. |
Dozens of operators perform mathematical functions, combine strings of characters together, and compare the values within variables. Consult the Visual Studio online help for a complete list of operators.
Understanding If-Then
A program often has to perform some action only if some condition is true. The Visual Basic structure that fulfills this requirement is the If-Then statement. It has the following structure:
If <condition> Then
<statements>
End If
The condition is evaluated and, if true, the code statements within the block execute. If the condition is false, these code statements are skipped. An alternate structure of the If-Then statement adds an Else clause:
If <condition> Then
<statements>
Else
<other statements>
End If
Like the first example, the condition is again evaluated, and the statements are run if it’s true. If the condition is false, then the code denoted in <other statements> runs instead. A final variant allows for multiple conditions to be evaluated:
If <condition> Then
<statements>
ElseIf <condition2> Then
<other statements>
[ElseIf <condition3> Then
<other statements>]
End If
The ElseIf clause allows you to test for more than one condition. You could, for example, execute one block of code if a condition evaluates a date as a weekday,asecond block if the date is determined to be a Saturday, and a third if the date is a Sunday. There’s no limit to the number of ElseIf blocks you can have, so you could execute one of seven blocks of code depending on each day of the week.
Understanding Loops
Many times, it’s necessary for a program to perform the same function more than once. The Basic language supports different types of loop structures. One commonly used loop is the For loop, which runs a fixed number of times:
Dim iVal As Integer
For iVal = 1 To 10
If iVal Mod 2 = 0 Then
Debug.WriteLine(iVal & " is even")
Else
Debug.WriteLine(iVal & " is odd")
End If
Next
This loop runs 10 times. The first time it runs, it assigns the integer variable iVal the value 1, then 2, then 3, all the way up to the value 10. Within the loop, the Mod (remainder) operator is performed against this variable. If the value of the variable divided by 2 has the remainder of 0, then the content of the variable is an even number, and the program outputs this fact. If the variable value divided by 2 has a remainder of 1, then the value is odd, and this is output.
Understanding Procedures and Functions
A programmer can break up code into smaller pieces of code known as either a procedure or a function. These smaller chunks of code can help to break up long stretches and make the code more readable. They can also reduce potentially duplicate code that needs to run from more than one place. Instead of having the same code in two places, the programmer can place the code into aprocedure or function and then call it from those two places.Listing A-1 performs the same function as the previous example, but it does so by using a function named IsAnEvenNumber that returns a True or False result. The function requires a value be sent to it that it can evaluate. The act of sending avalue to a function is called passing a parameter.Listing A.1: Using Procedures and Functions
Dim iVal As Integer
For iVal = 1 To 10
If IsAnEvenNumber(iVal) Then
Call OutputResult(iVal, "even")
Else
Call OutputResult(iVal, "odd")
End If
Next
Function IsAnEvenNumber(ByVal iVal As Integer) As Boolean
If iVal Mod 2 = 0 Then
Return True
Else
Return False
End If
End Function
Sub OutputResult(ByVal iVal As Integer, ByVal cResult As String)
Debug.WriteLine(iVal & " is " & cResult)
End Sub
After the function evaluates whether the passed-in value is even or odd, aprocedure named OutputResult writes the answer to the Debug window.
Note | A procedure differs from a function from the standpoint that it doesn’t return anything to the calling program. |