Thread: Confused

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339

    Question Confused

    Hey all,

    Ive just started learning c#, and im abit confused over the use of methods.

    Example is this code:
    Code:
    class Mdemo
    {
        public static void Main()
        {
            Mdemo ca = new Mdemo();
            Console.WriteLine("\nResult: {0}", ca.calc());
            Console.ReadLine();
        }
    
        int calc()
        {
            int a, b, c;
            Console.Write("Enter A: ");
            a = Int32.Parse(Console.ReadLine());
            Console.Write("Enter B: ");
            b = Int32.Parse(Console.ReadLine());
            c = a + b;
            return c;
        }
    
    }
    Ok i just made that and it works, but i dont see what the point is in the use of methods? From a C++ view, calc() is just a function, why instead of making an object reference to it, isnt it just called when needed like calc();. I heard something about it being because methods allow multiple instances of the same function to run at the same time, is this correct? I think i understand the code i just dont understand the reason a function has to be referenced to a sub object method to be used.

    Thanks for any tips,
    Jack
    TNT
    You Can Stop Me, But You Cant Stop Us All

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    You can make it work without an object reference by making calc() a static function. I don't have my compiler around, but I'm pretty sure this will work:
    Code:
    public static void Main()
    {
        Console.WriteLine("\nResult: {0}", calc());
        Console.ReadLine();
    }
    
    static int calc()
    {
        int a, b, c;
        Console.Write("Enter A: ");
        a = Int32.Parse(Console.ReadLine());
        Console.Write("Enter B: ");
        b = Int32.Parse(Console.ReadLine());
        c = a + b;
        return c;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  2. Confused
    By jeev2005 in forum C Programming
    Replies: 5
    Last Post: 06-01-2006, 02:04 PM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM