Main() in classes

  • Thread starter Thread starter Marc
  • Start date Start date
M

Marc

I am just starting out teaching myself C#, and I have come accross a
confusing point in my book. It seems that any class can have a Main()
function, but what does that mean? Is the Main() function for a class called
whenever an instance of that class is created, or would it be there for
another purpose?

I already know that one of the Main() functions can be asssigned to be entry
point for the entire program. However what class should this function be in?
I am coming from Cocoa on Mac OS X which seems to be organized very
similarly, yet very differently, from C#. In Cocoa, the owner of the
application has it's init method called when the program is first launched.
Is that what this is equivilent to in C#?

Thanks for any help.
- Marc
 
I already know that one of the Main() functions can be asssigned to be entry
point for the entire program. However what class should this function be in?

Doesn't matter which class as long as it has the [STAThread] attribute on it. You should have only one, I'm not sure what happens if
you have more than one, probably a compiler error.
Is the Main() function for a class called
whenever an instance of that class is created, or would it be there for
another purpose?

If you class is called Class1 then then function Class1 will be called when your class is created. This function is called the
constructor:

public class Class1
{
public Class1()
{
MessageBox.Show("Class1 created.");
}
}

I really consider the constructor to be nameless and the Class1 is the return value from the nameless function.
 
Marc said:
I am just starting out teaching myself C#, and I have come accross a
confusing point in my book. It seems that any class can have a Main()
function, but what does that mean? Is the Main() function for a class called
whenever an instance of that class is created, or would it be there for
another purpose?

I already know that one of the Main() functions can be asssigned to be entry
point for the entire program. However what class should this function be in?
I am coming from Cocoa on Mac OS X which seems to be organized very
similarly, yet very differently, from C#. In Cocoa, the owner of the
application has it's init method called when the program is first launched.
Is that what this is equivilent to in C#?


The purpose of the Main() method is to serve as an entry point to where a
program begins execution. While any class or struct may have a Main()
method, only one of those types contain the Main() that is called when the
program starts up.

For multiple types with Main(), you must explicitly designate one as holding
the entry point. The one you use is totally up to you - whatever makes
sense for your application. In VS.NET, you would right-click on the project
name and select Properties (or Project Menu/Properties) to bring up the
Program Property Pages dialog. Under Common Properties/General there is a
property called Startup Object where you can enter the type name with the
Main() method you want as the entry point. If you were compiling on the
command line, use the /main option, like this:

csc /main:SomeTypeName MyProgram.cs

Joe
 
Marc.... It is a potential entry point for an application. Do a unit
test in
main for a given component.

http://www.geocities.com/jeff_louie/OOP/oop5.htm

5) The Visual Studio IDE Startup Object is Empty By Default

If the startup object string is empty and you declare more than one
"Main" method in a Visual Studio project, the project will not compile.
This is very frustrating. Trying to set the startup object property in
the
Visual Studio IDE is non-trivial. Let me save you some pain. To set the
startup object, select View --> Solution Explorer. In the Solution
Explorer window select the name of the project. Now select View -->
Property Pages. (Do not select the Property Window!) Alternatively,
right
click on the project name and select Properties. Now select the Common
Properties folder in the left window. You can now select the appropriate
startup object from the drop down list. Don't forget to click on
"Apply."

Regards,
Jeff
It seems that any class can have a Main()
function, but what does that mean?<
 
Michael Culley said:
I already know that one of the Main() functions can be asssigned to be entry
point for the entire program. However what class should this function be in?

Doesn't matter which class as long as it has the [STAThread] attribute on it.

Well, you only need the [STAThread] attribute if you want the single
threaded apartment model for COM. This will be true for most Windows
Forms apps, but needn't be the case for other things.
You should have only one, I'm not sure what happens if
you have more than one, probably a compiler error.

You either need to have only one Main method, or tell the compiler
which class is the application entry point. I don't know what happens
if you have both Main() and Main(String[] args) though - should be easy
to test if anyone's sufficiently interested.

<snip>
 
Thanks for all of your help everyone! I figured that the constructor would
be the function called when a new instance of the object is created, but I
wasn't sure because the book I am using keeps using a Main() function in
each example class it creates.

It all seems so clear now. :-D

- Marc
 
Back
Top