Windows Develop Bookmark and Share   
 index > Windows Forms General > Sub Main declared twice?
 

Sub Main declared twice?

I have a Windows Forms application I'm trying to starup with a startup class.  Here's the signature of my main in the class:
<STAThread()> Public Shared Sub Main()

I have application framework disabled, but when I compile I get:
'Sub Main' is declared more than once in 'Foo': Foo.My.MyApplication.Main(Args() As String), Foo.Main.Main()

The app was originally running with a Startup form, but I'm trying to move to starting from Main().  Before I had it declared as shared and the 
<STAThread()> designation (ie, before .Net could find my Main()), the project would build, but I would get a "NoStartupFormException".

I have no Idea what's going on here.  How do I get rid of the Main() in MyApplication?  I would have assumed that disabling the app framework would do it, but apparently not.
cokert  Friday, January 06, 2006 2:12 PM

This is in VS 2005.  In your project properties, you can use the application framework, which provides some stuff like automatically saving configuration settings and XP Visual styles.  I'm not sure what else it does (haven't used it), and the reason I said I disabled it was that I had seen some other posts where the same problem had been solved by disabling the app framework.

I looked under start up object again and noticed that it had my class "Startup" at the bottom of the list, which had my shared sub Main() method.  I selected that as the start up item and it worked. 

I still don't know exactly what was going wrong.  I'm assuming by selecting "Sub Main" you're telling it to use My.MyApplication.Main(), which it then finds the other Main() that I had declared in the Startup class.  Maybe by setting the startup object to Startup, the compiler doesn't generate the Main() method in My.MyApplication?

Either way, my problem is solved, but thanks for your help.

cokert  Friday, January 06, 2006 8:41 PM

What do you mean you disabled the application framework?

In VB.NET, you can use either a startup form, OR a main method, but not both. If you just want to use a startup form, remove the Main method. If you want the Main method, set your startup object to Sub Main.

CommonGenius.com  Friday, January 06, 2006 8:14 PM

This is in VS 2005.  In your project properties, you can use the application framework, which provides some stuff like automatically saving configuration settings and XP Visual styles.  I'm not sure what else it does (haven't used it), and the reason I said I disabled it was that I had seen some other posts where the same problem had been solved by disabling the app framework.

I looked under start up object again and noticed that it had my class "Startup" at the bottom of the list, which had my shared sub Main() method.  I selected that as the start up item and it worked. 

I still don't know exactly what was going wrong.  I'm assuming by selecting "Sub Main" you're telling it to use My.MyApplication.Main(), which it then finds the other Main() that I had declared in the Startup class.  Maybe by setting the startup object to Startup, the compiler doesn't generate the Main() method in My.MyApplication?

Either way, my problem is solved, but thanks for your help.

cokert  Friday, January 06, 2006 8:41 PM
You cannot have two static subroutines named main anywhere in an assembly, even in different classes or namespaces. The reason for this is that when you run an exe, windows looks for the Sub Main as the entry point; if there were two, it wouldnt know where to start. When you set the startup object to a form, instead of Sub Main, Visual Studio automatically creates a static Sub Main within that form, which creates an instance of the form and calls Application.Run, passing the created instance. However, if you have defined another Sub Main somewhere else, you then have two Sub Mains, which is not allowed.
CommonGenius.com  Saturday, January 07, 2006 2:01 AM

You can use google to search for other answers

Custom Search

More Threads

• change color of selected row in datagridview
• Where Can I found the sample code for .jpg - >.SWF?
• UserControl accept only certain controls
• How to assign a shortcut key to a ToolStrip Button?
• Tooltip on UserControl
• Authentication in windows form applicaton.
• Alternative to OpenFileDialog to allow a user to select a path to file
• VB.NET 2k5 Java applet in webbrowser
• context menu transparent?
• Allow Key Accelerator on Tabs in TabControl