Can't start appliction with sub main

I tried to start my application off with module1, just as a test to see if modules worked the same. Here is my module

Module Module1
Sub main()
Dim frm As New Form1
frm.Show()
End Sub

End Module


Error 1 Protected types can only be declared inside of a class. C:\vs2005Projects\ModuleAttampt\ModuleAttampt\My Project\MyApplication.Designer.vb 22 29 ModuleAttampt

Error 2 'Module1' is a type in 'ModuleAttampt' and cannot be used as an expression. C:\vs2005Projects\ModuleAttampt\ModuleAttampt\My Project\MyApplication.Designer.vb 35 27 ModuleAttampt

Sub main()
Application.Run(New Form1)
End Sub

Same two errors.

When I chose sub main as the starting element instead of module1, I got this error:

Error 1 'Sub Main' is declared more than once in 'ModuleAttampt': ModuleAttampt.Module1.Main(), ModuleAttampt.My.MyApplication.Main(Args() As String)

But it is declared only once.

Module Module1
Sub Main()
Application.Run(New Form1)
End Sub

End Module

The documentation clearly states:

The Main procedure is the "starting point" for your application — the first procedure that is accessed when you run your code. Main is where you would put the code that needs to be accessed first. In Main, you can determine which form is loaded first when the program is started, find out if a copy of your application is already running on the system, establish a set of variables for your application, or open a database that the application requires. There are four varieties of Main: .....

Can somebody tell me how to open a form from sub Main?

dennist

[1701 byte] By [dennist] at [2008-1-23]
# 1
The Main method must be static (shared in VB) and must be marked with the Attribute <STAThread()>. And short example:


<STAThread()> _
Shared Sub Main()
' Starts the application.
Application.Run(New Form1())
End Sub

Questa at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

I tried
<STAThread()> _
Shared Sub Main()
' Starts the application.
Application.Run(New Form1())
End Sub

and the shared word was underlined, saying methods in a module cannot be declared shared.

dennist

dennist at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
Why you want to start from a Module and not making a StartUp class?
Questa at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
No particular reason. It's just in every previous version I've started up with a module. The documentation says we ought to be able to start with a module. So, I'd just like to know how to do it. Modules are a part of visual basic. I use visual basic. I'd just like to know how to do it. It really ought to be simple to call an opening form from a module.

dennist

dennist at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
Hi Dennist-

You can use a sub main() to start your application by disabling the application framework in the Project Designer:
- Double-click the My Project node in Solution Explorer -or- select Project->Properties
- Uncheck the "Enable application framework" checkbox
- Select Sub Main() as the startup object.

The error message we're returning here is pretty inpenetrable. I'll work with the feature team to see if we can provide more actionable feedback.

Thanks!

Joe Binder
Visual Basic Team

Joe_MS at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6
Thanks, Joe. However, I'm not going to disable the application framework just to use a sub main. I'll just do the stuff I did in the module in the opening form.

dennist

dennist at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7
what is the different if I have enable or disable application framework?
Romantic_touch at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8

If you start a project with a form, you can have "enable application framework" and then you can choose "enable xp styles".

If you start a project with Sub Main in a module, you can't have "enable application framework" then you can't choose "enable xp styles".
Then you must add (in the sub main) :
System.Windows.Forms.Application.EnableVisualStyles()

Dominique

DominiqueGratpain at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 9
what about the other options under Enable Application Framework?
Romantic_touch at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...