2

I have a class "A" from which I want to call a method in another different class "B" by passing a function as a parameter. Function passed as a parameter is in class B. So how to do it if I am calling method from class A?

I am using Visual Studio 2008 and .NET Framework 3.5.

I have seen this post but it tells how to call a main method by passing another method as a parameter but from the same class, not different class.

For example, in that post below example is provided:

public class Class1
{
    public int Method1(string input)
    {
        //... do something
        return 0;
    }

    public int Method2(string input)
    {
        //... do something different
        return 1;
    }

    public bool RunTheMethod(Func<string, int> myMethodName)
    {
        //... do stuff
        int i = myMethodName("My String");
        //... do more stuff
        return true;
    }

    public bool Test()
    {
        return RunTheMethod(Method1);
    }
}

but how to do the following:

public Class A
{
        (...)

        public bool Test()
        {
            return RunTheMethod(Method1);
        }

        (...)
}


public class B
{
    public int Method1(string input)
    {
        //... do something
        return 0;
    }

    public int Method2(string input)
    {
        //... do something different
        return 1;
    }

    public bool RunTheMethod(Func<string, int> myMethodName)
    {
        //... do stuff
        int i = myMethodName("My String");
        //... do more stuff
        return true;
    }
}
Community
  • 1
  • 1
Willy
  • 9,848
  • 22
  • 141
  • 284
  • Possible duplicate of [Pass Method as Parameter using C#](http://stackoverflow.com/questions/2082615/pass-method-as-parameter-using-c-sharp) – Steven Wood May 09 '17 at 09:40
  • @StevenWood It is not a duplicate, please read well my post before saying it is a duplicate. In the post you said, it provide a solution if you call RunTheMethod from the same class but what happens if you call RunTheMethod from another different class? Furthermore, the link you provided is already provided by me in the post, see the link in the post. – Willy May 09 '17 at 09:47

2 Answers2

4

You would need to make an instance of class B inside class A and then call the method, For example, change your class A to:

public Class A
{
        (...)
        private B myClass = new B();
        public bool Test()
        {
            return myClass.RunTheMethod(myClass.Method1);
        }

        (...)
}
Hristo
  • 1,805
  • 12
  • 21
  • Ok, but it works if methods in class B are public, if they are private it is impossible, right? – Willy May 09 '17 at 09:49
  • No, its still possible – Hristo May 09 '17 at 09:49
  • How? if for example Method1 is private, from class A and method Test you cannot do return myClass.RunTheMethod(myClass.Method1) because when you pass myClass.Method1, this is not visible. – Willy May 09 '17 at 10:03
  • 1
    Maybe [this](http://stackoverflow.com/questions/11900076/accessing-a-private-method-from-another-class) can help? – Hristo May 09 '17 at 10:06
1

Try this

public Class A
{
        (...)

        public bool Test()
        {
            var b = new B();
            return b.RunTheMethod(b.Method1);
        }

        (...)
}
LiverpoolOwen
  • 806
  • 2
  • 10
  • 25