Multiple Main() s in Petzold's book.

  • Thread starter Thread starter Victor
  • Start date Start date
V

Victor

In his book "Programming Microsoft Windows with C#", page 42, Petzold deals
with the case of a project with multiple Mains.

He explains that when compiling in the command line the argument /main can
be used, as in

csc prog1.cs prog2.cs /main:prog2ClassContainigMain (the code details are
mine)

Then he goes on to explain how to do the same thing when using Visual C#
..Net, or so I expected. He mentions to use the Add Existing Item dialog
box, and selecting the Link File option next to the Open button. However,
this does not deal with the issue of multiple Main() functions.

So my questions are:
1. Is it possible that VS does not have a way to deal with multiple
Main() functions?
2. Could it be that I am missing something?
3. Could it be that P. forgot to explain something?
4. If there is a way to use VS to have the same result as with the
command line (letting the project to use only one of the Main() functions),
what is it?

Thanks,
Victor
 
Hi, Victor
not sure about 1 - I don't use VS for long time already
2. You might need to consider that Main is just another method in class
declaration. As such, you can have Main in any class you design. Just when
compiling to exe csc has to know which of all present Main(s) should be used
as entry point in application, otherwise it is stupefied up to point of
failing to compile.
3. Nothing much relevant, I think
4. - see 1. Sorry, can't help here

HTH - at least in some points
Alex
 
4. If there is a way to use VS to have the same result as with the
command line (letting the project to use only one of the Main() functions),
what is it?

1) Open the project properties dialog box.
2) Navigate to open CommonProperties/General
3) Set the Startup Object property to the class name that contains the
preferred Main method
 
Victor said:
In his book "Programming Microsoft Windows with C#", page 42, Petzold deals
with the case of a project with multiple Mains.

He explains that when compiling in the command line the argument /main can
be used, as in

csc prog1.cs prog2.cs /main:prog2ClassContainigMain (the code details are
mine)

Then he goes on to explain how to do the same thing when using Visual C#
.Net, or so I expected. He mentions to use the Add Existing Item dialog
box, and selecting the Link File option next to the Open button. However,
this does not deal with the issue of multiple Main() functions.

So my questions are:
1. Is it possible that VS does not have a way to deal with multiple
Main() functions?
No.

2. Could it be that I am missing something?
Yes.

3. Could it be that P. forgot to explain something?
Yes.

4. If there is a way to use VS to have the same result as with the
command line (letting the project to use only one of the Main() functions),
what is it?

In the project properties, Common Properties, set the "Startup Object"
to the name of the type whose Main method should be called.
 
Victor said:
So my questions are:
1. Is it possible that VS does not have a way to deal with multiple
Main() functions?

VS can deal with mutliple main fonction: in the solution explorer, right
click on your project and choose Properties. Then go to Common Properties ->
General and set the Startup Object Property. There's a drop down list that
lists all the main functions you can choose from (in case there are multiple
main function in your project).
4. If there is a way to use VS to have the same result as with the
command line (letting the project to use only one of the Main() functions),
what is it?

See above
 

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

Back
Top