Cover | Table of Contents | Colophon
Module HelloWorld
' every console app starts with Main
Sub Main( )
System.Console.WriteLine("Hello World")
End Sub
End Module
Module HelloWorld
Module
keyword, as in the preceding code line. Likewise, you end each module
definition with this line:
End Module
Module HelloWorld
Module
keyword, as in the preceding code line. Likewise, you end each module
definition with this line:
End Module
Sub keyword to signal the beginning
of the subroutine and the End
Sub line to conclude the method:
Sub Main( )
System.Console.WriteLine("Hello World")
End Sub
"C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\vsvars32.bat"
Start -> Programs -> Microsoft Visual Studio .NET 2003 -> Visual Studio .NET Tools -> Visual Studio .NET Command Prompt
vbc HelloWorld.vb
Debug->Start
or just press F5. The program will compile and run to the breakpoint,
at which time it will stop and a yellow arrow will indicate the next
statement for execution, as in Figure 2-7.
If,
Do,
Do...While, and
For. Also discussed are operators, including the
assignment, logical, relational, and mathematical operators. Although
Visual Basic .NET is principally concerned with the creation and
manipulation of objects, it is best to start with the fundamental
building blocks: the elements from which objects are created. These
include the built-in types that are an intrinsic part of the Visual
Basic .NET language as well as the syntactic elements of Visual Basic
.NET.
Set
statement;
however properties have Set and Get accessors, as described in Chapter 5.
Option Strict On
Tools->Options->Projects->VB Defaults and
setting the default to Option Strict On.
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim myInteger As Integer = 7
Console.WriteLine("Initialized, myInteger: {0}", _
myInteger)
myInteger = 5
Console.WriteLine("After assignment, myInteger: {0}", _
myInteger)
End Sub
End Module
Output:
Initialized, myInteger: 7
After assignment, myInteger: 5myVariable = 5
myVariable = 5
Console.WriteLine("Hello World")
Dim x As Integer = 5
Dim x As Integer=5
Dimx As Integer = 5
Dim and the identifier x is
not extra, and is required. This is not
surprising; the whitespace allows the compiler to parse the keyword
Dim rather than some unknown term
Dimx. You are free to add as much or as little
whitespace between Dim and x as
you care to, but there must be at least one whitespace character
(typically a space or tab).
Dim x As Integer ' a statement x = 23 ' another statement Dim y As Integer = x ' yet another statement
Dim x As Integer = 23 : Dim y As Integer = 25
If,
Select
Case,
For, Do,
While, and For
Each. Iteration is discussed later in this
chapter, and For
Each is
considered in Chapter 3. For now,
let's consider some of the more basic methods of
conditional and unconditional branching.
Option Strict On
Imports System
Module Module1
Sub Main( )
Console.WriteLine("In Main! Calling SomeMethod( )...")
SomeMethod( )
Console.WriteLine("Back in Main( ).")
End Sub 'Main
Sub SomeMethod( )
Console.WriteLine("Greetings from SomeMethod!")
End Sub 'SomeMethod
End Module
Output:
In Main! Calling SomeMethod( )...
Greetings from SomeMethod!
Back in Main( ).
Do, For, and For
Each. You can also create a loop by using a statement
called Goto. This chapter considers the use of
Goto, Do, and
For. However, you'll have to wait
until Chapter 3 to learn more about
For
Each.
Goto statement is the seed from
which all other looping statements have been germinated.
Unfortunately, it is a semolina seed, producer of spaghetti code and
endless confusion.
Goto statements jump around a
great deal. Goto can cause your method to loop
back and forth in ways that are difficult to follow.
Goto statements, the
resulting morass of intersecting and overlapping lines might look
like a plate of spaghetti; hence the term "spaghetti
code." Spaghetti code is a contemptuous epithet; no
one wants to write spaghetti code.
Goto statement, but in the interest of
completeness, here's how you use it:
=, +, >,
&) that causes VB.NET to take an action. That
action might be an assignment of a value to a variable, the addition
of two values, a comparison of two values, concatenation of strings,
etc.
=)
has been used to assign a value to a variable:
Dim myVariable As Integer myVariable = 15
>) used to compare two values:
If valueOne > valueTwo Then
If statement compares valueOne with
valueTwo; if the former is larger than the latter, the test evaluates
true, and the If statement executes.
+, -, *,
/, and \), a sixth to return
the remainder when dividing integers (Mod), and a
seventh for exponential operations (^). The
following sections consider the use of these operators.
+),
subtraction (-), and multiplication
(*) operators work as you might expect. Adding two
numbers returns their sum, subtracting returns their difference, and
multiplying returns their product.
/ and \. The forward
slash or right-facing division operator (|
Operator
|
Given this statement:
|
The expressionevaluates to:
|
Logic
|
|---|---|---|---|
|
And
|
x = 3 And y = 7
|
False
|
Both must be true to evaluate true.
|
|
Or
|
x = 3 Or y = 7
|
True
|
Either or both must be true to evaluate true.
|
|
XOr
|
X = 5 XOr y = 7
|
False
|
True only if one (and only one) statement is true.
|
|
Not
|
Not x = 3
|
True
|
Expression must be false to evaluate true.
|
And
operator tests whether two statements
are both true. The first line in Table 3-3
includes an example that illustrates the use of the