Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions docs/visual-basic/misc/bc30738.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,44 @@ ms.assetid: c3212d87-6af3-4120-9e06-4d85fa910de2
---
# 'Sub Main' is declared more than once in '\<namespace>': \<message>

`Sub Main` can only be declared once inside a namespace.

**Error ID:** BC30738

## To correct this error

- Make sure there is only a single `Sub Main` procedure in your project.

`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 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 <xref:Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase> object and its events such as <xref:Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Startup>, <xref:Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.StartupNextInstance>, and <xref:Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Shutdown>. The application framework provides these events specifically for customizing application behavior without defining your own `Main` procedure.

## See also

- [Main Procedure in Visual Basic](../programming-guide/program-structure/main-procedure.md)
- [Overview of the Visual Basic Application Model](../developing-apps/development-with-my/overview-of-the-visual-basic-application-model.md)
- [-main (Visual Basic)](../reference/command-line-compiler/main.md)
- [Procedures](../programming-guide/language-features/procedures/index.md)