By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,384 Members | 845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,384 IT Pros & Developers. It's quick & easy.

Q: Main in VB.net

P: n/a
Hi

What is the closest equivalent to Main in a VB.Net form?

Geoff
Nov 21 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
"Geoff Jones" <no********@email.com> schrieb:
What is the closest equivalent to Main in a VB.Net form?


Inside your form class' code:

\\\
Public Shared Sub Main()
...
End Sub
///

In the project properties, select 'Sub Main' as the startup object.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2

P: n/a
Herfried,
In the project properties, select 'Sub Main' as the startup object. Or simply select the Form's class as the startup object. VB.NET will see the
Form class has a Shared Sub Main & use it, rather then implicitly adding
one.

Form example I normally have a MainForm class.

Public Class MainForm
Inherits System.Windows.Forms.Form

... designer generated code & other code

Public Shared Sub Main()
...
End Sub

End Class

Then in my Project Properties I have MainForm selected as the startup
object.

I'm not sure when I would ever actually select "Sub Main" as the startup
object...

Hope this helps
Jay
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eU**************@TK2MSFTNGP12.phx.gbl... "Geoff Jones" <no********@email.com> schrieb:
What is the closest equivalent to Main in a VB.Net form?


Inside your form class' code:

\\\
Public Shared Sub Main()
...
End Sub
///

In the project properties, select 'Sub Main' as the startup object.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3

P: n/a
Jay,

I see no real advantage in this
VB.NET will see the Form class has a Shared Sub Main & use it, rather
then implicitly adding one.


I am glad that at least is again leaved that way that the start point in a
program should be called "main".

So I am glad that it is implicitly added.

Not that important just to tell my thoughts about that.

Cor

Nov 21 '05 #4

P: n/a
Cor,
You see no real advantage in what?

Select one of:
A. Explicitly defining a Shared Sub Main
B. VB.NET implicitly creating a Main if you don't supply one
C. VB.NET using a Sub Main
D. Selecting the Form with a Sub Main as the startup object
E. Selecting Sub Main as the startup object
F. Defining Sub Main in a Form
G. Defining Sub Main in another Class
H. Defining Sub Main in a Module
I. Using "Main" as the name of the Startup routine
J. Some other obscure factoid

I really cannot make out what you don't see a real advantage to...

Thanks for any potential clarification you can make.

Jay
"Cor Ligthert" <no************@planet.nl> wrote in message
news:u5**************@TK2MSFTNGP10.phx.gbl...
Jay,

I see no real advantage in this
VB.NET will see the Form class has a Shared Sub Main & use it, rather
then implicitly adding one.


I am glad that at least is again leaved that way that the start point in a
program should be called "main".

So I am glad that it is implicitly added.

Not that important just to tell my thoughts about that.

Cor

Nov 21 '05 #5

P: n/a
Jay,

Nice multiple choise thanks and nice showing of all the possibilities as
well.

This answer from me only for a webform or windowform project, than I find B
the nicest and can not see a reason for all those others than that it is old
fashion use.(By a lot of people used as good practise).

And when that is the reason than that is no reason for me.

Cor
Cor,
You see no real advantage in what?

Select one of:
A. Explicitly defining a Shared Sub Main
B. VB.NET implicitly creating a Main if you don't supply one
C. VB.NET using a Sub Main
D. Selecting the Form with a Sub Main as the startup object
E. Selecting Sub Main as the startup object
F. Defining Sub Main in a Form
G. Defining Sub Main in another Class
H. Defining Sub Main in a Module
I. Using "Main" as the name of the Startup routine
J. Some other obscure factoid

Nov 21 '05 #6

P: n/a
Jay,

When there was not an implcitly sub main I would choise your solution by the
way.

That is A as I see it well.

Cor
Select one of:
A. Explicitly defining a Shared Sub Main
B. VB.NET implicitly creating a Main if you don't supply one
C. VB.NET using a Sub Main
D. Selecting the Form with a Sub Main as the startup object
E. Selecting Sub Main as the startup object
F. Defining Sub Main in a Form
G. Defining Sub Main in another Class
H. Defining Sub Main in a Module
I. Using "Main" as the name of the Startup routine
J. Some other obscure factoid


Nov 21 '05 #7

P: n/a
"Cor Ligthert" <no************@planet.nl> schrieb:
Nice multiple choise thanks and nice showing of all the possibilities as
well.

This answer from me only for a webform or windowform project, than I find
B the nicest and can not see a reason for all those others than that it is
old fashion use.(By a lot of people used as good practise).


I don't see why using 'Sub Main' should be old-fashion. Every application
has a starting point, and if the developer wants/needs to influence the
startup of the application, defining a 'Sub Main' is the way to go.

What I (personally) don't like is a 'Sub Main' defined inside a form.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #8

P: n/a
Herfried,
What I (personally) don't like is a 'Sub Main' defined inside a form. Does that go for other classes as well? For example Windows Services, puts
the Sub Main in the class derived from ServiceBase. When I define a system
tray application I normally put my Sub Main in the Main Component (the
object that manages the context menu & notify icon objects).

Do you normally define it in a Module? What do you normally call the module.

When I define my Sub Main in a module I normally call that module
MainModule. This is one of the few times I actually use a Module.

As I stated, I normally define my Sub Main in the MainForm, especially when
there is a "well-defined" MainForm...

Just wondering
Jay

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ey**************@TK2MSFTNGP11.phx.gbl... "Cor Ligthert" <no************@planet.nl> schrieb:
Nice multiple choise thanks and nice showing of all the possibilities as
well.

This answer from me only for a webform or windowform project, than I find
B the nicest and can not see a reason for all those others than that it
is old fashion use.(By a lot of people used as good practise).


I don't see why using 'Sub Main' should be old-fashion. Every application
has a starting point, and if the developer wants/needs to influence the
startup of the application, defining a 'Sub Main' is the way to go.

What I (personally) don't like is a 'Sub Main' defined inside a form.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #9

P: n/a
Jay,

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb:

That's a discussion about personal preference...
What I (personally) don't like is a 'Sub Main' defined inside a form. Does that go for other classes as well? For example Windows Services, puts
the Sub Main in the class derived from ServiceBase. When I define a system
tray application I normally put my Sub Main in the Main Component (the
object that manages the context menu & notify icon objects).

Do you normally define it in a Module? What do you normally call the
module.


I prefer a module if I want to explicitly control the entry point:

\\\
Public Module Program
Public Sub Main()
...
End Sub
End Module
///
When I define my Sub Main in a module I normally call that module
MainModule. This is one of the few times I actually use a Module.
It's similar for me. I try to use modules for my 'Sub Main' too (as always
with some exceptions).
As I stated, I normally define my Sub Main in the MainForm, especially
when there is a "well-defined" MainForm...


When there is a "well-defined" main form, it's ok to add the 'Sub Main'
directly to the form class. Nevertheless, when taking a look at the
projects which can be found on the web, it's often hard to locate the 'Sub
Main'/'void Main' there because people use unmeaningful names for their
classes/forms.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #10

P: n/a
Shared Sub Main

End Sub

in

Module.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #11

P: n/a
Herfried,

I don't see why using 'Sub Main' should be old-fashion. Every application
has a starting point, and if the developer wants/needs to influence the
startup of the application, defining a 'Sub Main' is the way to go.

What I (personally) don't like is a 'Sub Main' defined inside a form.

True, every application has a starting point however with the introduction
of the thirth generation programlanguages, that became more and more that
that should be named a kind of "Main".

That was in my opinion an easy way for the compiler builders to find that. A
little descripting way where you could say that a program was starting in a
certain method was leaved then. That is now reintroduced in VBNet, I find
that an improvement again.

A nice descripting way (as Jay as well suggest in this thread), is in my
opinion much nicer.
When that is in English it can be MainForm, however for me as well
StartForm, or whatever and when it is not in the English language by
instance PrimoPagina (to use a fictive language).

Just my thought of course.

Cor
Nov 21 '05 #12

P: n/a
Cor,
certain method was leaved then. That is now reintroduced in VBNet, I find
that an improvement again. "reintroduced"?

VB1 thru VB6 had the ability to define a "Sub Main" in a module to be used
as the entry point instead of an actual form. (Yes I just checked the VB1
manual :-))

VB.NET allows the Sub Main to be in a class or module as VB.NET allows
Shared Subs in classes.

Hope this helps
Jay
"Cor Ligthert" <no************@planet.nl> wrote in message
news:eq*************@TK2MSFTNGP09.phx.gbl... Herfried,

I don't see why using 'Sub Main' should be old-fashion. Every
application has a starting point, and if the developer wants/needs to
influence the startup of the application, defining a 'Sub Main' is the
way to go.

What I (personally) don't like is a 'Sub Main' defined inside a form.

True, every application has a starting point however with the introduction
of the thirth generation programlanguages, that became more and more that
that should be named a kind of "Main".

That was in my opinion an easy way for the compiler builders to find that.
A little descripting way where you could say that a program was starting
in a certain method was leaved then. That is now reintroduced in VBNet, I
find that an improvement again.

A nice descripting way (as Jay as well suggest in this thread), is in my
opinion much nicer.
When that is in English it can be MainForm, however for me as well
StartForm, or whatever and when it is not in the English language by
instance PrimoPagina (to use a fictive language).

Just my thought of course.

Cor

Nov 21 '05 #13

P: n/a
Herfried,
That's a discussion about personal preference... Yes this is largely a discussion of personal preference.
Nevertheless, when taking a look at the projects which can be found on the
web, it's often hard to locate the 'Sub Main'/'void Main' there because
people use unmeaningful names for their classes/forms. Agree.

Which is largely why I call the "main" form MainForm (with or without a Sub
Main) or put Sub Main in the MainModule.

In my Windows Service projects so far I have left in the Service's class
that is derived from ServiceBase... I really should move it to a
MainModule...

Hope this helps
Jay

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl... Jay,

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb:

That's a discussion about personal preference...
What I (personally) don't like is a 'Sub Main' defined inside a form.

Does that go for other classes as well? For example Windows Services,
puts the Sub Main in the class derived from ServiceBase. When I define a
system tray application I normally put my Sub Main in the Main Component
(the object that manages the context menu & notify icon objects).

Do you normally define it in a Module? What do you normally call the
module.


I prefer a module if I want to explicitly control the entry point:

\\\
Public Module Program
Public Sub Main()
...
End Sub
End Module
///
When I define my Sub Main in a module I normally call that module
MainModule. This is one of the few times I actually use a Module.


It's similar for me. I try to use modules for my 'Sub Main' too (as
always with some exceptions).
As I stated, I normally define my Sub Main in the MainForm, especially
when there is a "well-defined" MainForm...


When there is a "well-defined" main form, it's ok to add the 'Sub Main'
directly to the form class. Nevertheless, when taking a look at the
projects which can be found on the web, it's often hard to locate the 'Sub
Main'/'void Main' there because people use unmeaningful names for their
classes/forms.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #14

P: n/a
Thanks to all for a very interesting discussion!

Geoff

"Ravichandran J.V." <jv************@yahoo.com> wrote in message
news:eA**************@TK2MSFTNGP11.phx.gbl...
Shared Sub Main

End Sub

in

Module.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #15

P: n/a
Jay,
VB1 thru VB6 had the ability to define a "Sub Main" in a module to be used
as the entry point instead of an actual form. (Yes I just checked the VB1
manual :-))


Ahh that it is why Herfried wants to do it in his eyes maybe more grown up
language way, thinking it was an advantage of those languages.

For me is VBNet enough grown up to do it in that in my opinion more
descriptive way (when you do it right of course as we both write).

:-))

Thanks for waking me up (I knew it, just a stupidity writing it that way,
however the contexts stays for me).

Cor
Nov 21 '05 #16

P: n/a
Hello...

I also prefer startup in a module.
I normally have mine named modStartup.
I found it was easier and quicker when testing multiple forms to just
change the form that gets called then to constantly change it using the
project properties dialog.
Also makes it easy to add/modify application level attributes/variables
such as Application.EnableVisualStyles()etc...
I usually have mine auto generated for each new project (I modified the
project template VS uses)...

Just my thoughts on the matter.

Marc Cramer
Herfried K. Wagner [MVP] wrote:
Jay,

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb:

That's a discussion about personal preference...
What I (personally) don't like is a 'Sub Main' defined inside a form.


Does that go for other classes as well? For example Windows Services,
puts the Sub Main in the class derived from ServiceBase. When I define
a system tray application I normally put my Sub Main in the Main
Component (the object that manages the context menu & notify icon
objects).

Do you normally define it in a Module? What do you normally call the
module.

I prefer a module if I want to explicitly control the entry point:

\\\
Public Module Program
Public Sub Main()
...
End Sub
End Module
///
When I define my Sub Main in a module I normally call that module
MainModule. This is one of the few times I actually use a Module.

It's similar for me. I try to use modules for my 'Sub Main' too (as
always with some exceptions).
As I stated, I normally define my Sub Main in the MainForm, especially
when there is a "well-defined" MainForm...

When there is a "well-defined" main form, it's ok to add the 'Sub Main'
directly to the form class. Nevertheless, when taking a look at the
projects which can be found on the web, it's often hard to locate the
'Sub Main'/'void Main' there because people use unmeaningful names for
their classes/forms.

Nov 21 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.