Every VB 2008 executable application (such as a console program, Windows service, or a desktop GUI application) must contain a type defining a Main() method, which represents the entry point of the application. As you have just seen, the Main() method is typically placed within a module type, which as you recall implicitly defines Main() as a shared method.
Strictly speaking, however, Main() can also be defined within the scope of a class type or structure type as well. If you do define your Main() method within either of these types, you must explicitly make use of the Shared keyword. To illustrate, create a new Console Application named FunWithMain. Delete the code within the initial *.vb file and replace it with the following:
' Unlike Modules, members in a class are not ' automatically shared. Thus, we must declare Main() ' with the Shared keyword.
Shared Sub Main() End Sub End Class
If you attempt to compile your program, you will again receive a compiler error informing you that the Main() method cannot be located. Using the Application tab of the My Project dialog box, you can now specify Program as the startup object of the program (as previously shown in Figure 3-2).
Was this article helpful?