Since Visual Studio 2002, you can write applications that run in a Command Prompt window. The Command Prompt window isn’t really a DOS window, even though it looks like one. It’s a text window without Graphical User Interface (GUI), and the only way to interact with an application is to enter lines of text and read the output generated by the application, which is displayed on this text window, one line at a time. This type of application is called a Console application, and because information can be written to and read from the console window, this makes the console application a great way to learn new programming techniques without having to be concerned with the user interface.
Saying Hello using Console Application
To get started building your console application you choose File | New | Project from Visual Studio main menu, the New Project dialog box appears, choose Console Application. We’re going to demonstrate a simple console application that prompts the user to enter his or her name, and then it prints the appropriate message on a new line, as show in Figure 2 – 1.
Figure 2 - 1 A Console application uses the Command Prompt window to interact with the user
A Console application doesn’t have a user interface, so the first thing you’ll see is the code editor’s window with the following statements :
Sub Main(ByVal cmdArgs() As String)
End Sub
End Module
Unlike a Windows application, which is a class, a Console application is a module. Main () is the name of a subroutine that’s executed automatically when you run a Console application. The code you want to execute must be placed between the statements Sub Main () and End Sub. Insert the statements shown in below in the application’s Main () subroutine.
Sub Main()
Dim name As String
Console.Write("Enter your name: ")
name = Console.ReadLine
If name = "" Or name.Length = 0 Then
Console.WriteLine("Hello 'World'. Welcome to VB .NET")
Else
Console.WriteLine("Hello '" & name & "'. Welcome to VB .NET")
End If
Console.WriteLine()
Console.WriteLine("PRESS ANY KEY TO EXIT")
Console.ReadKey()
End Sub
End Module
This code is quite similar to the code of the equivalent Windows applications we developed earlier, except that it uses the Console. WriteLine statement to send its output to the Command Prompt window instead of a message box.
A Console application doesn’t react to events, because it has no visible interface. However, it’s easy to add elements of the Windows interface to a Console application. If you change the Console. WriteLine method calls into the MsgBox () function, the message will be displayed on a message box.
Four Varieties of Main
Every Visual Basic program must contain a procedure called Main. This procedure serves as the starting point and overall control for your application. It is called when your module is loaded.
There are four varieties of Main :
- Sub Main ()
- Sub Main (ByVal cmdArgs () As String)
- Function Main () As Integer
- Function Main (ByVal cmdArgs () As String) As Integer
Sub Main () Procedure
The most common variety of this procedure is Sub Main (). This is the simplest way is to declare a Sub procedure that does not take arguments or return a value.
Sub Main()
Dim frm As frmLogin
frm = New frmLogin()
frm.Show()
End Sub
End Module
Sub Main (ByVal cmdArgs () As String)
Main can also take a String array as an argument. Each string in the array contains one of the command - line arguments used to invoke your program. You can take different actions depending on their values. You can rename cmdArgs, but you can’t add new argument.
In this example, we’re going to demonstrate a simple math console application to show number of arguments, total of argument and average of agument, as show in Figure 2 – 2.
Figure 2 - 2 Simple math console application
To build console application above you choose File | New | Project from Visual Studio main menu, the New Project dialog box appears, choose Console Application. Change the text that appears on the Button control to ConsoleSimpleMath. Insert the statements shown in below in the application’s Main () subroutine.
Sub Main(ByVal cmdArgs() As String)
Dim counter, totalArg As Integer
Dim avg As Double
'Loop
For counter = 0 To cmdArgs.Length - 1
totalArg = totalArg + cmdArgs(counter)
Next
'Average
avg = totalArg / counter
Console.WriteLine("Total Argumen : " & counter)
Console.WriteLine("Total: " & totalArg)
Console.WriteLine("Average : " & avg)
End Sub
End Module
If you press the F5 to run the project, it will appears for a moment, but it will close automatically. To run the project properly, choose Build ConsoleSimpleMath from Build menu. Copy ConsoleSimpleMath. exe to c :\. Open the Command Prompt from windows Run dialog box. In the Command Prompt window, type the statement below :
C:\> ConsoleSimpleMath.exe 12 34 56 78 90 [ Enter ]
You will the result as shown on Figure 2 - 2.
Function Main () As Integer
You can declare Main to examine the command - line arguments that return an exit code, as follows.
Function Main() As Integer
Dim result, x, y As Double
Dim retval As Integer
Console.Write("x = ")
x = Console.ReadLine()
Console.Write("y = ")
y = Console.ReadLine()
result = x / y
If Double.IsInfinity(result) Or Double.IsNaN(result) Then
retval = 1
Else
retval = 0
End If
Console.WriteLine()
Console.WriteLine("{0}/{1} = {2}", x, y, result)
Console.WriteLine("return value = " & retval)
Console.ReadKey()
'exit code: 0 - succeeded; 1 - failed
Return retval
End Function
End Module
To build console application above you choose File | New | Project from Visual Studio main menu, the New Project dialog box appears, choose Console Application. Change the text that appears on the Button control to ConsoleReturnVal. If you press the F5 to run the project, it will appears as shown on Figure 2 - 3.
Figure 2 - 3 Console application can return an exit code
Function Main (ByVal cmdArgs () As String) As Integer
Now you can take a String array as an argument and also return an exit code, as follows.
Function Main(ByVal cmdArgs() As String) As Integer
Dim result As Double = 1
Dim counter, retval As Integer
'Loop
Try
For counter = 0 To cmdArgs.Length - 1
Console.Write("{0}) {1}", counter + 1, result)
result *= cmdArgs(counter)
Console.WriteLine(" * {0} = {1}", cmdArgs(counter), result)
Next
retval = 0
Catch ex As Exception
retval = 1
End Try
Console.WriteLine()
Console.WriteLine("result = {0}", result)
Console.WriteLine("return value = " & retval)
Console.ReadKey()
'exit code: 0 - succeeded; 1 - failed
Return retval
End Function
End Module
To build console application above you choose File | New | Project from Visual Studio main menu, the New Project dialog box appears, choose Console Application. Change the text that appears on the Button control to ConsoleReturnVal2. If you press the F5 to run the project, it will appears as shown on Figure 2 - 4.
Figure 2 - 4 Console application return 1 because 'A' is not numeric
1 comment:
Good post.
Post a Comment