| < Day Day Up > |
The following code listings are the final listings for the three examples in this chapter:
Listing 9.1 Single Line Example.
|
|
Module Module1 Sub Main() System.Console.WriteLine("Hello World!") End Sub End Module
|
|
Listing 9.2 Variable Example.
|
|
Module Module1 Sub Main() Dim str As String str = "Hello World!" System.Console.WriteLine(str) End Sub End Module
|
|
Listing 9.3 Imports Keyword Example.
|
|
Imports System.Console Module Module1 Sub Main() Dim str As String str = "Hello World!" WriteLine(str) End Sub End Module
|
|
| < Day Day Up > |
| < Day Day Up > |
In this chapter, we built our first VB .NET application. Although it is a simple example, it is a good basis for our activity in Chapter 10, Console Application Input/Output, which is to design a Console application that takes
| < Day Day Up > |
| < Day Day Up > |
Download CD Content
In the previous chapter, we spent some time
| Note |
The source code for the projects are located on the CD-ROM in the PROJECTS folder. You can either type them in as you go or you can copy the projects from the CD-ROM to your hard drive for editing. |
To start,
Figure 10.1:
Choose Console Application from the Templates list.
Next, we're going to add some code to the project.
The first line of code involves adding the Imports System.Console line to the application. This line must be present before Module Module1; your code should look like the following listing after you enter it:
Imports System.Console Module Module1 Sub Main() End Sub End Module
Next, we need to declare a couple of
Dim X,Y As Double
These variables are used to store the values that are being input by the user and should be placed after the Sub Main() line in the Code Editor. The code should look like the following list:
Imports System.Console Module Module1 Sub Main() Dim X, Y As Double End Sub End Module
Now, we're at a point to begin handling the input, but first, we need to let the user know that we are requesting information from them. We'll begin by using a
Write()
method to output text information, such as 'Please Enter First Number:'. Instead of
WriteLine()
, which actually creates an entire line with a
Here is the code with the Write() method added:
Imports System.Console Module Module1 Sub Main() Dim X, Y As Double Write("Please Enter First Number: ") End Sub End Module
Next, we're ready to handle the input from the user and store it in variable X . We can use the ReadLine() method to handle this input:
Imports System.Console Module Module1 Sub Main() Dim X, Y As Double Write("Please Enter First Number: ") X = ReadLine() End Sub End Module
We can repeat this same approach for output and input for variable Y :
Imports System.Console Module Module1 Sub Main() Dim X, Y As Double Write("Please Enter First Number: ") X = ReadLine() Write("Please Enter Second Number: ") Y = ReadLine() End Sub End Module
The final step for this section of the code is to add the numbers together and then display the output using the
Write()
and
WriteLine()
methods. We're using both
Imports System.Console Module Module1 Sub Main() Dim X, Y As Double Write("Please Enter First Number: ") X = ReadLine() Write("Please Enter Second Number: ") Y = ReadLine() Write("Your final answer is: ") WriteLine(X + Y) End Sub End Module
If you run the application using Debug Start Without Debugging, you'll see output similar to Figure 10.2.
Figure 10.2:
The code being executed.
We can check the input and output by entering values as prompted. Provide a value of 10 and then press the Enter key on the keyboard. Your window should now look like Figure 10.3.
Figure 10.3:
The program is asking for additional input.
Enter a value of 20 and then press the Enter key. This moves the program through the input, and then it displays the final answer of 30 , as seen in Figure 10.4.
Figure 10.4:
The answer is displayed.
Because we used a Double as the type of variable, you can also use decimal point values in the program. Try the following combinations:
X: -1.5
Y: 1.5
X: 1.2345
Y: -55.3433
X: 3.75
Y: 4.25
The values should all work equally and aren't dependent on positive or negative values.
| < Day Day Up > |