Edit

Share via


'Sub Main' is declared more than once in '<namespace>': <message>

Sub Main can only be declared once inside a namespace.

Error ID: BC30738

Cause

This error often occurs in Windows Forms applications when you define your own Sub Main procedure while the Visual Basic Application Framework is enabled. The application framework automatically generates a Main procedure, creating a conflict when you add another one.

To correct this error

Choose one of the following approaches:

  • Remove your custom Sub Main procedure.
  • Configure your startup form and initialization code using the application framework:
    • Set the Startup form in Project Properties > Application tab.
    • Use the My.MyApplication events for custom startup logic.

Option 2: Disable the application framework

If you need programmatic control over application startup (for example, to select which form to display based on command-line arguments):

  • In Project Properties > Application tab, uncheck Enable application framework.
  • Set Startup object to your module or class containing Sub Main.
  • Implement your own Sub Main procedure to control application startup.

Option 3: Remove duplicate Main procedures

  • Ensure there is only a single Sub Main procedure in your entire project.

Access My.MyApplication.Main

If you need to access the automatically generated startup code, you can work with the WindowsFormsApplicationBase object and its events such as Startup, StartupNextInstance, and Shutdown. The application framework provides these events specifically for customizing application behavior without defining your own Main procedure.

See also