The Main Procedure |
|
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.
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: Passing an Argument to Main() |
|
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:
|
|
||
Home | Copyright © 2005-2016, FunctionX | |
|