0

Asking the same question as Using delegates with non static methods [no picked answer] so as to bring a closure to it.

So I use @Adam Marshall's solution, it works, but as soon as I start using it, i.e., Testit():

using System;

public class TestClass
{
    private delegate void TestDelegate();
    TestDelegate testDelegate;

    public TestClass()
    {
        testDelegate = new TestDelegate(MyMethod);
    }

    public static void Testit()
    {
        testDelegate();
    }

    private virtual void MyMethod()
    {
        Console.WriteLine("Foobar");
    }
}

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        TestClass.Testit();
    }
}

It started to give the followig Error:

A object reference is required for the non-static field, method, or property

You can test it out here . How to fix it? (Please fix it if possible instead of directing me to other posts, I've read them but am not able to understand them) Thx.

1
  • 2
    I don't know how you'd get that error with this code. This is trying to use a non-static member from inside a static member which is not allowed. An instance is required to invoke an instance member. The fiddle produces an error saying that an instance is required. Commented Sep 19, 2017 at 23:14

2 Answers 2

2

Either everything has to be static or everything has to be instance. You're getting in trouble because you are mixing and matching.

Everything static:

using System;

public class TestClass
{
    private delegate void TestDelegate();
    static TestDelegate testDelegate;    //<-- static

    static TestClass()                   //<-- static
    {
        testDelegate = new TestDelegate(MyMethod);
    }

    public static void Testit()
    {
        testDelegate();
    }

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

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        TestClass.Testit();
    }
}

Everything instanced:

using System;

public class TestClass
{
    private delegate void TestDelegate();
    TestDelegate testDelegate;

    public TestClass()
    {
        testDelegate = new TestDelegate(MyMethod);
    }

    public void Testit()   
    {
        testDelegate();
    }

    private void MyMethod()
    {
        Console.WriteLine("Foobar");
    }
}

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        var t = new TestClass();
        t.Testit();   //<-- non-static
    }
}

Output (same in both examples):

Hello World
Foobar
Sign up to request clarification or add additional context in comments.

Comments

0

You could use action C# internal delegate. This way you do not have specify the delegate. Then in your static method you could new up your object.

using System;

public class TestClass
{

    Action testDelegate;

    public TestClass()
    {
        testDelegate = new Action(MyMethod);
    }

    public static void Testit()
    {
        TestClass ts = new TestClass();
        ts.testDelegate();
    }

    private void MyMethod()
    {
        Console.WriteLine("Foobar");
    }
}

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        TestClass.Testit();
    }
}

Output:

Hello World
Foobar

1 Comment

Thanks for the answer @StuartSmith, I picked the other only because that is one I can understand (I don't know Action)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.