Question What is the entry point of a VB program?

vgfigue

New member
Joined
Feb 21, 2012
Messages
2
Programming Experience
Beginner
I was playing with VB.Net yesterday using Visual Studio Express (VSE). I am trying to get a feel for the language. One thing that struck me was the fact that VB's entry point, Sub Main(), is written inside a Module, as opposed to inside a Class like in C#. At least that's how it appears in the books I'm reading. That got me thinking. Is it possible to change the entry point location from a Module to a Class, or for that matter to have Sub Main() completely outside these two constructs?

I search the internet and found out that VSE indeed allows one to changed the entry point through the project properties. So I set the entry point to Sub Main() and tried compiling the "Hello World!" project locating Sub Main() inside a Class construct. Well, that didn't work. I tried changing the name of the class to match the project name, but that didn't help. I didn't bother try putting Sub Main() completely outside a module or class figuring that would be impossible. However, while looking through various post in this forum, I got the feeling I gave up to soon. So here are my questions:

1. Can the entry point be within a Class/Form construct or completely outside any construct for that matter?
2. Can the entry point be some other Sub (e.g., Sub MyMain)

If the answer is yes to anyone of these questions, where do I set the option to make this work?

I'll take any help I can get.
Thanks!
 
A method must belong to a type, it can't be put directly in a namespace. A class or module (static/shared class) type would be natural. VS generates a Sub Main if you don't specify one. When application framework is enabled the entry point is a generated Sub Main method in a MyApplication class in project (My.Application points to that) that inherits WindowsFormsApplicationBase class. When application framework is disabled and you select a form as startup object the entry point is a generated Sub Main in that form class if you haven't declared one, or you can select the type that contains your custom Sub Main. This method must be Shared when put in a class, in a module all members is implicitly Shared.
Main Procedure in Visual Basic
 
You can also simply pass execution from the entry point or constructor to whatever you want your main routine to be, I highly doubt a single call will ruin your day. I usually take advantage of class constructors to setup defaults anyways.
 
One thing that struck me was the fact that VB's entry point, Sub Main(), is written inside a Module, as opposed to inside a Class like in C#.
That's not really a difference. A module is just a VB language construct. When compiled, a VB module becomes a static class. So, a VB module is equivalent to a C# static class.
 
Thanks guys. I think you've answered my question. I realize as one of you said that in C# the sub Main() starts in a class as a static member. I went back and placed sub Main() in a class with the shared keyword and it worked. (I wish they would have just chose static instead of shared, but oh well.) So Sub Main() can actually go in a class or module construct after all, but is not valid in a namespace.
 
I wish they would have just chose static instead of shared, but oh well.
Static means something else in VB.
So Sub Main() can actually go in a class or module construct after all, but is not valid in a namespace.
It's just a method and methods are only valid as members of types. There is nothing special or unusual about a Main method in this regard. The only thing that's valid in a namespace is a type because, by definition, a namespace is a logical grouping of types where each type name is unique.
 
Back
Top