Skip to content

Commit 1792fc9

Browse files
CopilotBillWagnergewarren
authored
Enhance BC30738 error documentation with Windows Forms and My.MyApplication guidance (dotnet#48539)
* Initial plan * Enhance BC30738 error documentation with Windows Forms and My.MyApplication guidance Co-authored-by: BillWagner <[email protected]> * Apply suggestions from code review * Use xref syntax for My.MyApplication and events Co-authored-by: BillWagner <[email protected]> * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: Bill Wagner <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent a764da0 commit 1792fc9

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 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.
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+
- Remove your custom `Sub Main` procedure.
29+
- 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+
- In **Project Properties** > **Application** tab, uncheck **Enable application framework**.
38+
- Set **Startup object** to your module or class containing `Sub Main`.
39+
- 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+
## Access `My.MyApplication.Main`
46+
47+
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.
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)