按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
your code or edit your user interface。 The work area will display only a single piece of informa
tion; which could be some code; a user interface; or a project。 As you saw earlier; when you
double…click Form1。vb in the Solution Explorer; the work area displays the form related to the
Form1。vb file。
Now that you have an idea of how the IDE works; let’s continue with our examples。 Next
up is the console application。
Creating the Console Application
A console application is a text…based application。 This means that rather than displaying a GUI;
it uses a mand…line interface。
The console has a very long history because the console was the first way to interact with a
puter。 Consoles are not very user…friendly and bee very tedious for any plex oper
ations; yet some people claim that a console is all you need。 (See http://en。wikipedia。org/
wiki/mand_line_interface for more information about the console。)
Writing to the console works only if the currently running application has a console。 To
open the console in Windows; select Start Run and type cmd in the dialog box。 When you test
a console application; Visual Basic Express opens a console for you。
Visual Basic Express can create; build; and manage console applications。
Adding a Console Application Project to the Solution
We will now create an application that outputs the text “hello; world” to the console。 Follow
these steps to add the new project to the ThreeExamples solution:
1。 Choose File Add New Project。
2。 Make sure the location is the same as that of WindowsApplication。
3。 Select Console Application and change the name to ConsoleApplication。
The Solution Explorer changes to show the additional project and now also shows the
solution。 The work area displays the source code。
Notice the simplicity of the console application。 It contains a single; plain…vanilla source
code file; called Module1。vb。 Console applications typically do not have any specialized group
ings and do not have any events。
…………………………………………………………Page 35……………………………………………………………
C H AP TE R 1 ■ R E AD Y ; ST E AD Y ; G O! 13
Making the Console Application Say Hello
To make the console application do something; you need to add some source code to the
Main() method; as follows:
Module Module1
Sub Main()
Console。WriteLine(〃hello; world〃)
Console。ReadKey()
End Sub
End Module
The bolded line writes the text “hello; world” to the console。
If you tried to run the console application by pressing Ctrl+F5; you would instead cause
the Windows application; WindowsApplication; to run。 Let’s change that next。
Setting the Startup Project
To execute the console application; you need to set the console application as the startup project。
Did you notice how the WindowsApplication project is in bold in the Solution Explorer? That
means WindowsApplication is the startup project。 Whenever you run or debug an application;
the startup project is executed or debugged。
To switch the startup project to ConsoleApplication; right…click the ConsoleApplication
project and select Set As StartUp Project。 ConsoleApplication will now be in bold; meaning it is
the startup project of the ThreeExamples solution。
Running the Console Project
With ConsoleApplication set as the startup project; you can now press Ctrl+F5 to run the
console application。 The output is as follows:
hello; world
Executing the console application does not generate a window; as you saw with the Windows
application。 Instead; a mand prompt is started with ConsoleApplication as the application
to execute。 Executing that application generates the text “hello; world。” You will also see that
you can press any key to close the mand prompt window。 Visual Basic Express automati
cally generated the code to show this output and execute this action。
In general; the console application is limited; but it’s an easy way to run specific tasks。
Now let’s move on to the next example。
…………………………………………………………Page 36……………………………………………………………
14 CH AP T E R 1 ■ R E A DY ; ST E A DY ; G O !
Creating the Class Library
The third example in this chapter is not a application; rather; it is a shareable piece of
functionality; typically called a class library。 Windows applications and console applications
are programs that you can execute from a mand prompt or Windows Explorer。 A class
library cannot be executed by the user; but needs to be accessed by a Windows application or
console application。 It is a convenient place to put code that can be used by more than one
application。
Adding a Class Library Project to the Solution
We will now create a class library for the Windows application and console application to
share。 Follow these steps to add the new project to the ThreeExamples solution:
1。 Right…click the solution name; ThreeExamples; in the Solution Explorer。
2。 Select Add New Project。
3。 Select Class Library and change the name to ClassLibrary。
The resulting solution project should look like Figure 1…8。
Figure 1…8。 Updated solution structure that contains three projects
The added ClassLibrary project has a single file called Class1。vb; which is a