C# Entry Point

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcfly1204
    New Member
    • Jul 2007
    • 233

    C# Entry Point

    I understand that C# console applications need an entry point of Main(), and that Main() can return and integer or nothing. My question is, what do people typically put in this method? I understand that the method is needed otherwise the compiler will not know where to start, but it kind of seems like a waste. For instance, I am working on part of an EDI and the first task is to read the contents of a directory into an array, but this does not seem appropriate for the Main() method. I realize this is probably just my inexperience asking, but any input would be greatly appreciated. Thanks.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You can use it to instanciate any classes you need, setup and directory structures etc.
    Al though generally it would be more like
    Code:
    int main(string[] args)
    {
       //if required:
       //do something based upon the provided arguments
    
       initfunction();
       somefunction();
       //maybe a loop
       //
       return 0;
    }
    and you would have it call out to other functions, rather then perform operations directly in it

    Comment

    • mcfly1204
      New Member
      • Jul 2007
      • 233

      #3
      Originally posted by Plater
      You can use it to instanciate any classes you need, setup and directory structures etc.
      Al though generally it would be more like
      Code:
      int main(string[] args)
      {
         //if required:
         //do something based upon the provided arguments
      
         initfunction();
         somefunction();
         //maybe a loop
         //
         return 0;
      }
      and you would have it call out to other functions
      So, if I wanted it to return an integer, I could have it check the directory for files, if no files were found, the application would quit. If files were found, then the application would move onto the next method?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Plater
        You can use it to instanciate any classes you need, setup and directory structures etc.
        Al though generally it would be more like
        Code:
        int main(string[] args)
        {
           //if required:
           //do something based upon the provided arguments
        
           initfunction();
           somefunction();
           //maybe a loop
           //
           return 0;
        }
        and you would have it call out to other functions, rather then perform operations directly in it
        <nitpicking>
        The entry point has got to be static
        </nitpicking>

        @OP If you don't include the entry point then how are you going to specify where your program starts?

        Comment

        • mcfly1204
          New Member
          • Jul 2007
          • 233

          #5
          Originally posted by r035198x
          <nitpicking>
          The entry point has got to be static
          </nitpicking>

          @OP If you don't include the entry point then how are you going to specify where your program starts?
          If you read what I wrote originally, you will see that I am aware of the compiler needing the entry point to know where to begin. If you keep reading you will see that my question is, logically, what else can you do with this entry point? i.e. this obviously begins the application, but how can I use it to move on through my code?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by mcfly1204
            If you read what I wrote originally, you will see that I am aware of the compiler needing the entry point to know where to begin. If you keep reading you will see that my question is, logically, what else can you do with this entry point? i.e. this obviously begins the application, but how can I use it to move on through my code?
            I just didn't like the part where you said "but it kind of seems like a waste".
            You shouldn't be doing much in the Main method anyway. The best programs simply use it to instantiate an object of some class which drives the whole application.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Originally posted by r035198x
              <nitpicking>
              The entry point has got to be static
              </nitpicking>
              Hehe, oops, was I going with good old c++ there.


              And don't get so caught up on the return integer. As far as I know it was really on used back in the days of DOS to set the ERRORLEVEL enivronment variable for use in batch file scripts and possibly just to notify the shell that the program "failed" at whatever it wanted to do.

              I don't even think most .NET projects have you return an int. Mine auto-declare main() as void

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by Plater
                Hehe, oops, was I going with good old c++ there.
                ...
                I guessed as much. I get obsessed with nitpicking sometimes ...

                Comment

                • mcfly1204
                  New Member
                  • Jul 2007
                  • 233

                  #9
                  After some reading I see that returning an integer is helpful to indicate success or failure, which I am not concerned with. I also know that I am not trying to pass any value from the command line, so the parameters will be blank. So I understand how I will structure the method, but what could I place in the method that would begin the application. I don't know, specific to my case, what I would instantiate.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by mcfly1204
                    After some reading I see that returning an integer is helpful to indicate success or failure, which I am not concerned with. I also know that I am not trying to pass any value from the command line, so the parameters will be blank. So I understand how I will structure the method, but I could place in the method that would begin the application.
                    I wouldn't use that return value for indicating success or failure either. There are far better and more user friendly options of providing that information ...

                    Comment

                    • mcfly1204
                      New Member
                      • Jul 2007
                      • 233

                      #11
                      I think I understand a little better now. Here is what I went with:

                      namespace P3A4
                      {

                      class MainDriver
                      {

                      static void Main()
                      {
                      XMLSelect select = new XMLSelect();

                      select.SelectFi les();
                      select.InsertSL X();

                      }

                      class XMLSelect
                      {

                      public void SelectFiles()
                      {
                      ......

                      So basically, the class that the Main method is contained in does not have to be descript as far as the application is concerned. However, you then can instantiate another class as an object, such as XMLSelect, and begin to call methods that will begin the application. Correct thinking?

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Correct thinking.

                        Comment

                        • Plater
                          Recognized Expert Expert
                          • Apr 2007
                          • 7872

                          #13
                          The default behavior for a windows project in vs2005 is to make a class called "program"
                          and all it has in it is it's static main function that launches the main form.
                          I generally don't even add anything to that file.

                          Comment

                          Working...