What is the main method of a VB.NET program?

Public Sub Main() indicates the entry point of VB.Net program. Every Visual Basic application must contain a procedure called Main. This procedure serves as the starting point and overall control for your application. The .NET Framework calls your Main procedure when it has loaded your application and is ready to pass control to it. Unless you are creating a Windows Forms application, you must write the Main procedure for applications that run on their own.

Main contains the code that runs first. In Main, you can determine which form is to be loaded first when the program starts, find out if a copy of y our application is already running on the system, establish a set of variables for your application, or open a database that the application requires.

Requirements for the Main Procedure

A file that runs on its own (usually with extension .exe) must contain a Main procedure. A library (for example with extension .dll)

does not run on its own and does not require a Main procedure. The requirements for the different types of projects you can create are as follows:

  • Console applications run on their own, and you must supply at least one Main procedure. .
  • Windows Forms applications run on their own. However, the Visual Basic compiler automatically generates a Main procedure in such an application,

and you do not need to write one.

  • Class libraries do not require a Main procedure. These include Windows Control Libraries and Web Control Libraries. Web applications are

deployed as class libraries.

Declaring the Main Procedure

There are four ways to declare the Main procedure. It can take arguments or not, and it can return a value or not.

If you declare Main in a class, you must use the Shared keyword. In a module, Main does not need to be Shared.

  • The simplest way is to declare a Sub procedure that does not take arguments or return a value.

Module mainModule
     Sub Main()
          MsgBox("The Main procedure is starting the application.")
         ' Insert call to appropriate starting place in your code.
          MsgBox("The application is terminating.")
     End Sub
End Module

  • Main can also return an Integer value, which the operating system uses as the exit code for your program.
  • Other programs can test this code by examining the Windows ERRORLEVEL value. To return an exit code,
  • you must declare Main as a Function procedure instead of a Sub procedure.

Module mainModule
     Function Main() As Integer
          MsgBox("The Main procedure is starting the application.")
          Dim returnValue As Integer = 0
          ' Insert call to appropriate starting place in your code.
          ' On return, assign appropriate value to returnValue.
          ' 0 usually means successful completion.
          MsgBox("The application is terminating with error level " &
          CStr(returnValue) & ".")
          Return returnValue
     End Function
End Module

  • 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.


Module mainModule
     Function Main(ByVal cmdArgs() As String) As Integer
          MsgBox("The Main procedure is starting the application.")
          Dim returnValue As Integer = 0
          ' See if there are any arguments.
          If cmdArgs.Length > 0 Then
               For argNum As Integer = 0 To UBound(cmdArgs, 1)
                    ' Insert code to examine cmdArgs(argNum) and take
                    ' appropriate action based on its value.
               Next argNum
          End If
          ' Insert call to appropriate starting place in your code.
          ' On return, assign appropriate value to returnValue.
          ' 0 usually means successful completion.
          MsgBox("The application is terminating with error level " &
          CStr(returnValue) & ".")
          Return returnValue
     End Function
End Module

  • You can declare Main to examine the command-line arguments but not return an exit code, as follows.

Module mainModule
     Sub Main(ByVal cmdArgs() As String)
          MsgBox("The Main procedure is starting the application.")
          Dim returnValue As Integer = 0
          
          ' See if there are any arguments.
          If cmdArgs.Length > 0 Then
               For argNum As Integer = 0 To UBound(cmdArgs, 1)
                    ' Insert code to examine cmdArgs(argNum) and take
                    ' appropriate action based on its value.
               Next argNum
          End If
          
          ' Insert call to appropriate starting place in your code.
          MsgBox("The application is terminating.")
     End Sub
End Module


Related Videos:

MiltonMarketing.com Related Videos.

Related Posts:

Microsoft .NET Help Documents on VB.NET main procedure

How to find the main() entry point in a VB.Net winforms app?

Remote code execution vulnerability discovered in WordPress

Learn about JavaScript return statement

FAQs

How to create a constant in VB.NET?

Where is the main function in VB.Net

Designing an app in Pseudo code

Python Online Compiler Code on the Go

Methods of teaching programming

Introduction to Python Programming Language

Introduction to JavaScript – Create a Variable: let

Google investing $2.1m into kw programs supporting women in computer science, coding for youth

Why Python is the cooler programming language

Introduction to JavaScript – Control Flow: if/else Statements

My little pony learning game in VB.NET

Introduction to JavaScript

Adding Python Comments

The 14 most popular programming languages, according to a study of 100,000 developers

Who is this Android App Development course for?

Defining What an Application Is

Automotive company Magna invests $5 million in AI systems

Talking to Your Computer

Learn Modules and Packages in Python programming

Magna’s new MAX4 self-driving platform offers autonomy up to Level 4

CSS, HTML, JAVASCRIPT, Online Compiler. Code on the go by replit.it

Introduction to JavaScript – Review Types and Operators

Hello World Android app built with Android Studio

Exception Handling in Python programming

Ontario based – Magna Adds to its All-New Electric Vehicle from INEOS Automotive

Introduction to JavaScript – CONSOLE

Introduction to JavaScript – Variables

Android Studios

What is the use of New Keyword in VB.NET?