locked
Basic Console application code RRS feed

  • Question

  • Oddly enough I've only written a couple of (simple)  console apps in the past so I'm pretty ignorant about them. The Main method is static which doesn't allow me to call non-static methods. Seems a bit limiting. I was thinking I would move the entry point to an object method as follows - could someone please advise if this is a bad idea? Or maybe there is a better way go about this? Or maybe it's just not worthwhile? 

      static void Main(string[] args) {
                new entryPoint(args);
            }

             private class entryPoint {
                 private string[] args;
                 public entryPoint(string []args) {
                     this.args = args;
                        //Put the body of application code here.
                 }
             }

    Thursday, November 20, 2014 4:12 PM

Answers

  • >>The Main method is static which doesn't allow me to call non-static methods

    You could just create a class, define your method(s) in there and then simply create in instance of this a class in the static Main method to be able to call the instance method(s):

     
     class Program
      {
        static void Main(string[] args) {
    
          MyApplicationClass obj = new MyApplicationClass();
          obj.SomeMethod();
        }
      }
    
      public class MyApplicationClass
      {
        public void SomeMethod() {
          //do something...
        }
      }
    
    This is the normal behaviour.

    However the actual main entry point of a C# program must be static and it must be named "Main". Please refer to the following page for more information about this: http://msdn.microsoft.com/en-us/library/acy3edy3.aspx

    Please remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by jal2 Friday, November 21, 2014 1:58 PM
    Thursday, November 20, 2014 4:19 PM
  • That is the way to do it in my opinion. Main is a holdover from the way UNIX was designed for C.

    The only thing I would do differently is create a constructor that take a string[] argument so the args can be used in MyApplicationClass.

      public class MyApplicationClass
      {
        private string[] MyArgs;
    
        public MyApplicationClass(string[] args) {
          MyArgs = args;
        }
        public void SomeMethod() {
          //do something...
        }
      }


    Dan Randolph - My Code Samples List

    • Marked as answer by jal2 Friday, November 21, 2014 2:00 PM
    Friday, November 21, 2014 1:38 AM
  • I replicated the code but tidy up a bit in VS and I got no problem though. In the same namespace you cannot put the access modifier in front of the class.

    Copy and paste the code to your VS it should work straightaway.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ToDelete
    {
        class Program
        {
            static void Main(string[] args)
            {
                EntryObject eo = new EntryObject();
            }
        }
    
        class EntryObject
        {
            private string[] args;
    
            public EntryObject()
            { }
            public EntryObject(string[] args)
            {
                this.args = args;
            }
        }
    }

    chanmm


    chanmm

    • Marked as answer by jal2 Friday, November 21, 2014 2:00 PM
    Friday, November 21, 2014 3:06 AM
  • Hi jal2,

    Based on your code and description, I think you might not need to post “args” from Main to object method, you could try the code as below:

            static void Main(string[] args)
            {
                new entryPoint();
            }
    
            private class entryPoint
            {
                public entryPoint() {
                    //Put the body of application code here.
                    //Console.WriteLine("hello");
                    //Console.ReadKey();
                }
            }

    If you have any further questions, please feel free to post in this forum.

    Best Regards,

    Leo


    • Edited by Leo ZT Friday, November 21, 2014 9:50 AM
    • Marked as answer by jal2 Friday, November 21, 2014 2:00 PM
    Friday, November 21, 2014 9:49 AM

All replies

  • >>The Main method is static which doesn't allow me to call non-static methods

    You could just create a class, define your method(s) in there and then simply create in instance of this a class in the static Main method to be able to call the instance method(s):

     
     class Program
      {
        static void Main(string[] args) {
    
          MyApplicationClass obj = new MyApplicationClass();
          obj.SomeMethod();
        }
      }
    
      public class MyApplicationClass
      {
        public void SomeMethod() {
          //do something...
        }
      }
    
    This is the normal behaviour.

    However the actual main entry point of a C# program must be static and it must be named "Main". Please refer to the following page for more information about this: http://msdn.microsoft.com/en-us/library/acy3edy3.aspx

    Please remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by jal2 Friday, November 21, 2014 1:58 PM
    Thursday, November 20, 2014 4:19 PM
  • That is the way to do it in my opinion. Main is a holdover from the way UNIX was designed for C.

    The only thing I would do differently is create a constructor that take a string[] argument so the args can be used in MyApplicationClass.

      public class MyApplicationClass
      {
        private string[] MyArgs;
    
        public MyApplicationClass(string[] args) {
          MyArgs = args;
        }
        public void SomeMethod() {
          //do something...
        }
      }


    Dan Randolph - My Code Samples List

    • Marked as answer by jal2 Friday, November 21, 2014 2:00 PM
    Friday, November 21, 2014 1:38 AM
  • I replicated the code but tidy up a bit in VS and I got no problem though. In the same namespace you cannot put the access modifier in front of the class.

    Copy and paste the code to your VS it should work straightaway.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ToDelete
    {
        class Program
        {
            static void Main(string[] args)
            {
                EntryObject eo = new EntryObject();
            }
        }
    
        class EntryObject
        {
            private string[] args;
    
            public EntryObject()
            { }
            public EntryObject(string[] args)
            {
                this.args = args;
            }
        }
    }

    chanmm


    chanmm

    • Marked as answer by jal2 Friday, November 21, 2014 2:00 PM
    Friday, November 21, 2014 3:06 AM
  • Hi jal2,

    Based on your code and description, I think you might not need to post “args” from Main to object method, you could try the code as below:

            static void Main(string[] args)
            {
                new entryPoint();
            }
    
            private class entryPoint
            {
                public entryPoint() {
                    //Put the body of application code here.
                    //Console.WriteLine("hello");
                    //Console.ReadKey();
                }
            }

    If you have any further questions, please feel free to post in this forum.

    Best Regards,

    Leo


    • Edited by Leo ZT Friday, November 21, 2014 9:50 AM
    • Marked as answer by jal2 Friday, November 21, 2014 2:00 PM
    Friday, November 21, 2014 9:49 AM