Main() in classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc

    #1

    Main() in classes

    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


  • Joe Mayo

    #2
    Re: Main() in classes

    "Marc" <macfreak2000@h otmail.com> wrote in message
    news:%23b5aJJVi DHA.2748@TK2MSF TNGP11.phx.gbl. ..[color=blue]
    > 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[/color]
    called[color=blue]
    > 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[/color]
    entry[color=blue]
    > point for the entire program. However what class should this function be[/color]
    in?[color=blue]
    > 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[/color]
    launched.[color=blue]
    > Is that what this is equivilent to in C#?[/color]


    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:SomeTypeNa me MyProgram.cs

    Joe
    --
    Learn C# with step-by-step tutorials and practical examples. Start your first program, explore lists and parsing, and understand how .NET runs your code.



    Comment

    • Michael Culley

      #3
      Re: Main() in classes

      > I already know that one of the Main() functions can be asssigned to be entry[color=blue]
      > point for the entire program. However what class should this function be in?[/color]

      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.
      [color=blue]
      > Is the Main() function for a class called
      > whenever an instance of that class is created, or would it be there for
      > another purpose?[/color]

      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.

      --
      Michael Culley


      Comment

      • Jeff Louie

        #4
        Re: Main() in classes

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

        Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


        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[color=blue]
        >It seems that any class can have a Main()[/color]
        function, but what does that mean?<

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

        Comment

        • Jon Skeet

          #5
          Re: Main() in classes

          Michael Culley <mculley@NOSPAM optushome.com.a u> wrote:[color=blue][color=green]
          > > 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?[/color]
          >
          > Doesn't matter which class as long as it has the [STAThread] attribute on it.[/color]

          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.
          [color=blue]
          > You should have only one, I'm not sure what happens if
          > you have more than one, probably a compiler error.[/color]

          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>

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Marc

            #6
            Re: Main() in classes

            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


            Comment

            • Marc

              #7
              Re: Main() in classes

              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


              Comment

              Working...