Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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:
Option 1: Use the application framework (recommended for most Windows Forms apps)
- Remove your custom
Sub Mainprocedure. - Configure your startup form and initialization code using the application framework:
- Set the Startup form in Project Properties > Application tab.
- Use the
My.MyApplicationevents 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 Mainprocedure to control application startup.
Option 3: Remove duplicate Main procedures
- Ensure there is only a single
Sub Mainprocedure 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.