You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 'Sub Main' is declared more than once in '\<namespace>': \<message>
13
13
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):
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
+
22
49
## See also
23
50
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)
0 commit comments