4

Possible Duplicate:
Call non-static method from static method c#

We can call non-static method from static method creating instance. Code:

public class Foo
{
    public void Data1()
    {
    }

    public static void Data2()
    {
        Foo foo = new Foo();
        foo.Data1();
    }
}

However, I heard that non-static method can be called from static method with the help of delegate. is it true? If yes, then how? Please guide me with sample code. Thanks.

Community
  • 1
  • 1
Mou
  • 15,673
  • 43
  • 156
  • 275
  • 3
    From: http://stackoverflow.com/questions/1360183/call-non-static-method-from-static-method-c Duplicate? – Julius F Jul 16 '11 at 09:18

7 Answers7

10

This is one method for calling a non-static method via a delegate. Note that it is a two-step process, since to call a non-static method, you absolutely need an instance of the class. I would also note that there is almost certainly a better way to do what you want to do, since needing to call a non-static method from a static method despite not wanting to use an object instance makes it sound like the non-static method should be static.

public class MyClass
{
    private static Action NonStaticDelegate;

    public void NonStaticMethod()
    {
        Console.WriteLine("Non-Static!");
    }

    public static void CaptureDelegate()
    {
        MyClass temp = new MyClass();
        MyClass.NonStaticDelegate = new Action(temp.NonStaticMethod);
    }

    public static void RunNonStaticMethod()
    {
        if (MyClass.NonStaticDelegate != null)
        {
            // This will run the non-static method.
            //  Note that you still needed to create an instance beforehand
            MyClass.NonStaticDelegate();
        }
    }
}
dlev
  • 48,024
  • 5
  • 125
  • 132
3

To call a non static method you need an instance of the object - there is no way around that.

Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
  • Why we be able to call the non static method inside a static method via class instance ?? Class instance does not transform it into static, does it ?? – Zeeshan Ajmal Dec 14 '16 at 12:00
3

Yes, you can call a non-static method from anywhere using a delegate, but you need the instance of the class to create the delegate, so that the instance is available when the method is called.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    no one give the code to make it possible to call non-static method from static method through delegate. please give me a sample code. thanks. – Mou Jul 16 '11 at 09:25
2

You will always need an instance to call an instance method. It is possible to have a delegate that points to a method on an instance - but then you still have an instance that are being used in the call, just indirectly.

driis
  • 161,458
  • 45
  • 265
  • 341
2

You can do other clever things, but the reality is that along the way somewhere you have to create or access an instance of the non-static object. You could pass a delgate into the method, that refers to an instance of the method ( rather, a method on an instance of the non-static object ), but that would have had to be created outside your code.

Schroedingers Cat
  • 3,099
  • 1
  • 15
  • 33
  • no one give the code to make it possible to call non-static method from static method through delegate. please give me a sample code. thanks. – Mou Jul 16 '11 at 16:41
2

In the code provided you don't call non stati method in static one, but you call instance public method of another class, this is different story.

So in your case I would suggest write something like:

static void Data2(Foo foo).

Regards.

Tigran
  • 61,654
  • 8
  • 86
  • 123
2

This way like you did it can be called a non-static method from the static method. To call a non-static method from a static method, you have to create an new reference of the class in which the non-static method is in. So your Data1 method is in Foo class, you have to create a new referenc (Foo foo = new Foo()) to be allowed to go out of static method.

Mitja Bonca
  • 4,268
  • 5
  • 24
  • 30