Newbie: Main instantiating the class it's contained within ???

H

hdjim69

Hello, every book an article I read on csharp, the Main method
instantiates the actual class it's contained within. Seems weird to
me. Kinda having a hard time with this. Why do this ? Why not have
Main in it's own class like MainStarter or something and then
instantiate all the other classes. What is the advantage to having a
Main method in a some class and then have it instantiate that class.

TIA
J
 
J

Jesse Houwing

Hello hdjim69,
Hello, every book an article I read on csharp, the Main method
instantiates the actual class it's contained within. Seems weird to
me. Kinda having a hard time with this. Why do this ? Why not have
Main in it's own class like MainStarter or something and then
instantiate all the other classes. What is the advantage to having a
Main method in a some class and then have it instantiate that class.

Even if you make a class named MainStarter, you need a class from somewhere.

Some call it Program, others Executable, others Main or Application. There's
a 1000 options at least, so your pick is as good as any.

Visual Studio 2005 and later seem to default to Program (at least in C#).
 
A

Alberto Poblacion

hdjim69 said:
Hello, every book an article I read on csharp, the Main method
instantiates the actual class it's contained within. Seems weird to
me. Kinda having a hard time with this. Why do this ? Why not have
Main in it's own class like MainStarter or something and then
instantiate all the other classes. What is the advantage to having a
Main method in a some class and then have it instantiate that class.

You don't need to do that. Main is a static method, and as long as all
the functionality of the program can be provided by static resources it
doesn't have to instantiate anything. However, if you want to do some
object-oriented programming, and therefore you wish to instantiate objects
in one or more classes, you will have to provide those classes somewhere.
For convenience, the simplest code samples just place Main inside the same
class that they are going to instantiate, since it needs to be contained
somewhere. But of course you can write Main inside a MainStarter class and
never instaintiate that class if that is the way you want to organize your
code.
 
H

hdjim69

But of course you can write Main inside a MainStarter class and
This is what I mean. Why not just have a MainStarter or MainEntry
class, no need to instantiate THAT class but the Main method
instantiates whatever it needs to get the app running.

Is this not common?

Thx

J
 
H

hdjim69

Is this not common?
Also, if Main is in some class that gets instantiated, what's to
prevent other classes from calling Main over and over again. This
seems like it could be a problem.

J
 
P

Peter Duniho

This is what I mean. Why not just have a MainStarter or MainEntry
class, no need to instantiate THAT class but the Main method
instantiates whatever it needs to get the app running.

This happens all the time. The Visual Studio Designer template for a
Windows Forms-based application creates a class named Program, in which
the Main() method is contained. This class is almost never instantiated,
and only instantiates other classes for the purpose of executing the
program code.
Is this not common?

It's quite common.

And...

Also, if Main is in some class that gets instantiated, what's to
prevent other classes from calling Main over and over again. This
seems like it could be a problem.

Whether the class gets instantiated or not has nothing to do with whether
other classes could call Main() over and over again. Main() is a static
method and can be called whether or not the containing class is
instantiated.

A correct program will not ever call Main(). So the answer is to write
correct programs rather than incorrect ones. Instantiation of the class
containing Main() is a red herring.

Pete
 
H

hdjim69

Main() is a static method and can be called whether or not the containing class is instantiated.

yes, I thought about that after I sent the last post. Main is an
entry point and should never really be called....

also, I see lots of examples in books where, within the class it
instantiates itself. is this common also? isn't that an endless
loop?


Thanks.

J
 
J

Jon Skeet [C# MVP]

hdjim69 said:
the containing class is instantiated.

yes, I thought about that after I sent the last post. Main is an
entry point and should never really be called....

If you make it private, that helps to avoid it being called from
elsewhere...
also, I see lots of examples in books where, within the class it
instantiates itself. is this common also? isn't that an endless
loop?

Why would it be an endless loop? It's only endless if *every* instance
of the class creates another one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top