Skip to content

Commit 09285fc

Browse files
CopilotBillWagner
andcommitted
Enhance BC30738 error documentation with Windows Forms and My.MyApplication guidance
Co-authored-by: BillWagner <[email protected]>
1 parent 4010b3b commit 09285fc

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

docs/visual-basic/misc/bc30738.md

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,44 @@ ms.assetid: c3212d87-6af3-4120-9e06-4d85fa910de2
1111
---
1212
# 'Sub Main' is declared more than once in '\<namespace>': \<message>
1313

14-
`Sub Main` can only be declared once inside a namespace.
15-
16-
**Error ID:** BC30738
17-
18-
## To correct this error
19-
20-
- Make sure there is only a single `Sub Main` procedure in your project.
21-
14+
`Sub Main` can only be declared once inside a namespace.
15+
16+
**Error ID:** BC30738
17+
18+
## Cause
19+
20+
This error commonly occurs in Windows Forms applications when you attempt to 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.
21+
22+
## To correct this error
23+
24+
Choose one of the following approaches:
25+
26+
### Option 1: Use the Application Framework (Recommended for most Windows Forms apps)
27+
28+
1. Remove your custom `Sub Main` procedure.
29+
1. Configure your startup form and initialization code using the application framework:
30+
- Set the **Startup form** in **Project Properties** > **Application** tab.
31+
- Use the `My.MyApplication` events for custom startup logic.
32+
33+
### Option 2: Disable the Application Framework
34+
35+
If you need programmatic control over application startup (for example, to select which form to display based on command-line arguments):
36+
37+
1. In **Project Properties** > **Application** tab, uncheck **Enable application framework**.
38+
1. Set **Startup object** to your module or class containing `Sub Main`.
39+
1. Implement your own `Sub Main` procedure to control application startup.
40+
41+
### Option 3: Remove duplicate Main procedures
42+
43+
- Ensure there is only a single `Sub Main` procedure in your entire project.
44+
45+
## Accessing My.MyApplication.Main
46+
47+
If you need to access the automatically generated startup code, you can work with the `My.MyApplication` 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.
48+
2249
## See also
2350

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

0 commit comments

Comments
 (0)