Home

The Main Procedure

 

Introduction

So far, we have mostly used only one version of the Main() procedure. In reality, there are various flavors available for Main(). The Main() procedure is considered as the entry point of a program. In fact, a program, that is an executable program, starts by, and stops with, the Main() procedure. The way this works is that, at the beginning, the compiler enters the Main() procedure in a top-down approach. If you want the user to provide additional information when executing your program, you can take care of this in the Main() function as this is the entry point of your program.

 

Practical Learning Practical Learning: Introducing the Command Line

  1. In Notepad, start a new empty file and type the following:
     
    Imports System
    
    Module Exercise
        Public Sub main()
            Dim FirstName As String = "James"
            Dim LastName As String = "Weinberg"
            Dim WeeklyHours As Double = 36.5
            Dim HourlySalary As Double = 12.58
    
            Dim FullName As String = LastName & ", " & FirstName
            Dim WeeklySalary As Double = WeeklyHours * HourlySalary
    
            Console.WriteLine("Employee Payroll")
            Console.WriteLine("Full Name:    {0}", FullName)
            Console.WriteLine("WeeklySalary: {0}", WeeklySalary.ToString("C"))
        End Sub
    End Module
  2. Save the file in a new folder named CommandLine1
  3. Save the file itself as Exercise.vb in the CommandLine1 folder
  4. To test the application, open the Command Prompt and change to the CommandLine 1folder
  5. Type vbc Exercise.vb and press Enter
  6. To execute the program, type the name Exercise and press Enter. This would produce:
     
    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.
    
    C:\Documents and Settings\Jacques Zoo>CD\
    
    C:\>CD VBasic\CommandLine1
    
    C:\VBasic\CommandLine1>vbc Exercise.vb
    Microsoft (R) Visual Basic .NET Compiler version 7.10.3052.4
    for Microsoft (R) .NET Framework version 1.1.4322.573
    Copyright (C) Microsoft Corporation 1987-2002. All rights reserved.
    
    
    C:\VBasic\CommandLine1>Exercise
    Employee Payroll
    Full Name:    Weinberg, James
    WeeklySalary: $459.17
    
    C:\VBasic\CommandLine1>
  7. Return to your text editor
 

Command Request from Main()

So far, to compile a program, we simply typed the vbc command at the command prompt. Then, to execute a program, we typed its name at the prompt. If we distributed one of these programs, we would tell the user to type the name of the program at the command prompt. In some cases, you may want the user to provide additional information besides the name of the program. To request additional information from the user, you can pass a string argument to the Main() function. The argument should be passed as an array and make sure you provide a name for the argument. Here is an example:

Imports System

Module Exercise
    Public Sub main(ByVal Args() As String)
        
    End Sub
End Module

The reason you pass the argument as an array is so you can use as many values as you judge necessary. To provide values at the command prompt, the user types the name of the program followed by each necessary value. Here is an example:

The values the user would provide are stored in the zero-based argument array without considering the name of the program. The first value (that is, after the name of the program) is stored at index 0, the second at index 1, etc. Based on this, the first argument is represented by Args[0], the second is represented by Args[1], etc.

Since the array argument is based on the Array class of the System namespace, if you want to find out how many values the user supplied, you can call the Array.Length property.

Each of the values the user types is a string. If any one of them is not a string, you should/must convert its string first to the appropriate value.

  

 

Practical Learning Practical Learning: Passing an Argument to Main()

  1. To pass an argument to Main(), change the file as follows:
     
    Imports System
    
    Module Exercise
        Public Sub main(ByVal Args() As String)
            Dim FirstName As String
            Dim LastName As String
    	Dim FullName As String
            Dim WeeklyHours As Double
            Dim HourlySalary As Double
    	Dim WeeklySalary As Double
    
    	FirstName    = Args(0)
            LastName     = Args(1)
    	WeeklyHours  = CDbl(Args(2))
    	HourlySalary = CDbl(Args(3))
    
            FullName     = LastName & ", " & FirstName
            WeeklySalary = WeeklyHours * HourlySalary
    
            Console.WriteLine("Employee Payroll")
            Console.WriteLine("Full Name:    {0}", FullName)
            Console.WriteLine("WeeklySalary: {0}", WeeklySalary.ToString("C"))
        End Sub
    End Module
  2. Save the file and switch to the command prompt
  3. To compile the application, type vbc Exercise.vb and press Enter
  4. To execute the program, type Exercise followed by a first name, a last name, and two decimal values. An example would be Exercise Hermine McComb 38.00 15.85
     
  5. Return to your text editor

 

 

The Main() Function

So far, we have used Main() only as a sub procedure. You can also use it as a function, that is, a procedure that returns a value. To do this, on the left side of Main, type Function. On the right side of its parentheses, type As Integer. You can also use Main() as a function that takes an array as argument. Based on this, the four versions available for Main() are:

  • Sub Main()
  • Sub Main(ByVal Args() As String)
  • Function Main() As Integer
  • Function Main(ByVal Args() As String) As Integer
 

Home Copyright © 2005-2016, FunctionX