#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    54
    Rep Power
    3

    C# How to call a private method


    Hi I'm very new to c#.

    I am trying to call a private method Area with the parameters I want and print it out. I am getting an error message and believe I am calling it wrongly. The codes as follows. Please advice. Thank you.

    Code:
    using System;
    
    namespace Area
    {
        class Program
        {
            static void Main(string[] args)
            {
                double answer = Area(10);
                Console.WriteLine(answer);
                Console.ReadLine();
            }
    
            private double Area(int radius)
            {
                double pi = 3.14;
                return pi * radius * radius;              
            }
                           
        }
    }
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    As you've said Area is a private method, but it's also an instance method. You need an actual instance of the class (Program) to be able to call it.

    Make the Area method static. Like Main.

IMN logo majestic logo threadwatch logo seochat tools logo