CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2009
    Posts
    3

    Question about main function

    I have often seen code something similar to
    class Program
    {
    static void Main(string[] Args)
    {
    (new Program()).run();
    }
    public void run();
    {
    //code generally seen in main function;
    }
    }

    Can anyone tell me what is the purpose for following such a method of coding and what are the advantages disadvantages for the same?

  2. #2
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Question about main function

    Is it in C# or in Java ?

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Question about main function

    It is defined standard which you must obey if you want you application to run.
    Quote Originally Posted by MSDN
    The Main method is the entry point of a C# console application or windows application. (Libraries and services do not require a Main method as an entry point.). When the application is started, the Main method is the first method that is invoked.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Question about main function

    Quote Originally Posted by boudino View Post
    It is defined standard which you must obey if you want you application to run.
    You know, I thought the same thing at first, but I think that the OP is more concerned with the code inside of main, i.e., (new Program()).run(). I have never seen anyone do that before...

  5. #5
    Join Date
    Oct 2009
    Posts
    3

    Re: Question about main function

    the code is in C#

    you can find similar code at http://www.java2s.com/Code/CSharp/De...romconsole.htm

    here is a sample application for reading input from 2 numbers posted at above site
    using System;
    using System.Collections.Generic;
    using System.Text;

    class Program {
    static void Main(string[] args) {
    (new Program()).run();
    }

    public void run() {
    double dailyRate = readDouble("Enter your daily rate: ");
    int noOfDays = readInt("Enter the number of days: ");
    writeFee(calculateFee(dailyRate, noOfDays));
    }

    private void writeFee(double p) {
    Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
    }

    private double calculateFee(double dailyRate, int noOfDays) {
    return dailyRate * noOfDays;
    }

    private int readInt(string p) {
    Console.Write(p);
    string line = Console.ReadLine();
    return int.Parse(line);
    }

    private double readDouble(string p) {
    Console.Write(p);
    string line = Console.ReadLine();
    return double.Parse(line);
    }
    }

  6. #6
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Question about main function

    I'm using similar code quite often, even I preferre to keep Program class just with Main() function and create a new class for the application logick.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  7. #7
    Join Date
    Oct 2009
    Posts
    3

    Re: Question about main function

    Thanks Boudino,
    I read some where that such a method keeps code safe. So in that case, my main function will contain a statement
    (new NewClass()).run()
    or should I be using
    NewClass tempnewclass = new NewClass();

    Are there any requirements that my new class should be static or of any other type....?

    -harsh

  8. #8
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Question about main function

    No.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured