Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Func with instance method

Tags:

c#

public class x : y
{    

public Func<user, bool> SendStuffAction = SendStuff;

//overridden from y
public override bool SendStuff(user u)
{
  //do stuff
}

}

Taking the above code, where SendStuff is an local overridden instance method, I get a context error that SendStuff not being static. Can't a delegate point to an instance method from inside the same class to which the method SendStuff exists?

Error is:cannot access static method in a non-static context

What if the method is private, why would it not work then.

private Func<user, bool> SendStuffAction = SendStuff;
like image 582
Chris G Avatar asked Jun 22 '10 16:06

Chris G


1 Answers

Yes, it can...but, you need to set it in the constructor if you do not declare as static.

class MyClass
{
   public Func<loan, user, bool> SendStuffAction ;

   MyClass()
   {
      SendStuffAction = SendStuff;
   }

   bool SendStuff(loan loanVar, user userVar)
   {
      return true;
   }
}
like image 87
Eric Dahlvang Avatar answered Sep 19 '22 13:09

Eric Dahlvang