1

Why running this C# code shows error on F1(); in Main?

namespace Project1
{
    public partial class Program1
    {       
        private void F1()
        {
            Console.WriteLine("F2");
        }

        private void F2()
        {
            Console.WriteLine("F1");
        }

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

This is a console application. It works if I define an object of class Program1. But when I try this on a Windows Form Application, I can put F1(); in a button_click event and it runs without error without defining an object of Class Form1?

Phoenix
  • 1,045
  • 1
  • 14
  • 22
NESHOM
  • 899
  • 16
  • 46

2 Answers2

3

You have not defined the methods as static. Therefore you need to create an instance of your class first and then call them using that instance.

If you want to call the methods directly, you could make them static. In this case since you seem to be just displaying static text it would be fine to do so. However, often, methods will actually need to act on instances and so must be called as such. You may want to look at this question, which discusses when it makes sense to make your methods static.

Community
  • 1
  • 1
shree.pat18
  • 21,449
  • 3
  • 43
  • 63
2

Below are two alternatives:

namespace Project1
{
public partial class Program1
{       
    private void F1()
    {
        Console.WriteLine("F1");
    }

    private void F2()
    {
        Console.WriteLine("F2");
    }

    static void Main(string[] args)
    {
        var program1 = new Program1();
        program1.F1();
    }
}
}

OR...

namespace Project1
{
public partial class Program1
{       
    static private void F1()
    {
        Console.WriteLine("F1");
    }

    static private void F2()
    {
        Console.WriteLine("F2");
    }

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

BTW, I changed your WriteLine text to reflect the function being called.

mdebeus
  • 1,928
  • 3
  • 18
  • 27
  • Thanks for your answer, but why these methods without static work when I put them in a "windows form application"? I can call them for example in button clicked event without them being declared as static? – NESHOM Jun 22 '14 at 15:24
  • Program1 is a partial class - that means there is some other file that contains another set of functions that are part of this Program1 class. If the button clicked event function is part of your Program1 class (perhaps in the other file), and it is not static, then it can call non-static functions within the class. – mdebeus Jun 22 '14 at 20:48
  • I am not talking about the above code. I am talking about when you create a new Windows Form Application with a button on the form. If I copy paste "Function F1" from the above code somewhere close to button_clicked event, I can call F1() within button clicked event. So the question is that why it works there and not in console application? – NESHOM Jun 22 '14 at 20:53
  • 1
    The difference is that your button clicked event is NOT static, while the main function in the console application IS static. You CANNOT call a non-static function in a static function without an instance of the class that owns the non-static function. – mdebeus Jun 23 '14 at 05:51
  • Check out this MSDN page on static vs instance members: http://msdn.microsoft.com/en-us/library/aa645629(v=vs.71).aspx – mdebeus Jun 23 '14 at 06:03