Datasheet
10
❘
CHAPTER 1 VISUAL STUDIO 2010
You may have noticed that in all the sample code presented thus far, each line is a complete command. If
you ’ re familiar with another programming language, then you may be used to seeing a specifi c character
that indicates the end of a complete set of commands. Several popular languages use a semicolon to
indicate the end of a command line.
Visual Basic doesn ’ t use visible punctuation to end each line. Traditionally, the BASIC family of languages
viewed source fi les more like a list, whereby each item on the list is placed on its own line. At one point the term
was source listing . By default, Visual Basic ends each source list item with the carriage - return linefeed, and treats
it as a command line. In some languages, a command such as
X = Y can span several lines in the source fi le until
a semicolon or other terminating character is reached. Thus previously, in Visual Basic, that entire statement
would be found on a single line unless the user explicitly indicates that it is to continue onto another line.
To explicitly indicate that a command line spans more than one physical line, you ’ ll see the use of the
underscore at the end of the line to be continued. However, one of the new features of Visual Basic 10, which
ships with Visual Studio 2010, is support for an implicit underscore when extending a line past the carriage -
return linefeed. However, this new feature is limited as there are still places where underscores are needed.
When a line ends with the underscore character, this explicitly tells Visual Basic that the code on that
line does not constitute a completed set of commands. The compiler will then continue to the next line to
fi nd the continuation of the command, and will end when a carriage - return linefeed is found without an
accompanying underscore.
In other words, Visual Basic enables you to use exceptionally long lines and indicate that the code has been
spread across multiple lines to improve readability. The following line demonstrates the use of the underscore
to extend a line of code:
MessageBox.Show("Hello World", "A Message Box Title", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Prior to Visual Basic 10 the preceding example illustrated the only way to extend a single command line
beyond one physical line in your source code. The preceding line of code can now be written as follows:
MessageBox.Show("Hello World", "A Message Box Title",
MessageBoxButtons.OK, MessageBoxIcon.Information)
The compiler now recognizes certain key characters like the “ , ” or the “ = ” as the type of statement where a
line isn ’ t going to end. The compiler doesn ’ t account for every situation and won ’ t just look for a line extension
anytime a line doesn ’ t compile. That would be a performance nightmare; however, there are several logical
places where you, as a developer, can choose to break a command across lines and do so without needing to
insert an underscore to give the compiler a hint about the extended line.
Finally, note that in Visual Basic it is also possible to place multiple different statements on a single line, by
separating the statements with colons. However, this is generally considered a poor coding practice because
it reduces readability.
Console Applications
The simplest type of application is a console application . This application doesn ’ t have much of a user
interface; in fact, for those old enough to remember the MS - DOS operating system, a console application
looks just like an MS - DOS application. It works in a command window without support for graphics
or input devices such as a mouse. A console application is a text - based user interface that displays text
characters and reads input from the keyboard.
The easiest way to create a console application is to use Visual Studio. For the current discussion let ’ s just
look at a sample source fi le for a Console application, as shown in the following example. Notice that the
console application contains a single method, a
Sub called Main . By default if you create a console application
in Visual Studio, the code located in the Sub Main is the code which is by default started. However, the
Sub Main isn ’ t contained in a class, instead the Sub Main that follows is contained in a Module :
Module Module1
Sub Main()
Console.WriteLine("Hello World")
CH001.indd 10CH001.indd 10 4/5/10 11:56:14 AM4/5/10 11:56:14 AM