Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all, I am attempting to do something like this:

I have a class called "Jobs". Inside that class I have a method called "Update". I am wanting to pass a method as a parameter with zero arguments. See example below.

Ex:


Class Job

C#
public Job(){
   //
}

public void Update(wantToPassMethodHere){
   //Execute wantToPassMethodHere method
}


Main Class

C#
private Job job = new Job();

public void Update(){
   job.Update(WriteMessage());
}


public void WriteMessage(){
   Console.WriteLine("hi");
}


Is this possible?

What I have tried:

I have tried researching the issue, couldn't find a valid solution.
Posted
Updated 23-Aug-16 21:24pm
Comments
[no name] 23-Aug-16 23:34pm    
Delegates - https://msdn.microsoft.com/en-us/library/ms173172.aspx Just Google your exact question.

Yes It is possible, use Delegates Tutorial (C#)[^]

refer this example:
C#
public delegate void MyMethod();
public delegate int AnotherMethodWithParams(int a);

public class MyClass
{
    public MyClass ()
	{
        var obj =  new Job();
        obj.Update(DoSomething);
       int answer =  obj.Square(Square);
       
	}
    void DoSomething() { 
        // your code
    }
    int Square(int a) { return a * a; }
}


 
public class Job
{ 

    public void Update(MyMethod method)
    {
        method.Invoke();
    }
    public int Square(AnotherMethodWithParams method)
    {
       return method.Invoke(2);
    }
}
 
Share this answer
 
Yes, It is possible, you can achieve your desire goal by using
C#
Action Delegate


About Action Delegate
You can use this delegate to pass a method as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have no parameters and no return value. (In C#, the method must return void. In Visual Basic, it must be defined by the Sub…End Sub construct. It can also be a method that returns a value that is ignored.) Typically, such a method is used to perform an operation.

EXAMPLE:-

Class Job

C#
public class job
{
  public void update(Action wantToPassMethodHere)
  {
      wantToPassMethodHere();
   }
}


Main Class
C#
public class Program
{
  public static void testMethod()
  {
     Console.WriteLine("Hello Test Method");
  }
  public static void Main(string[] args)
  {
    //Here you can instantiate your job class and called update method
    
     job jb = new job();
     jb.update(testMethod);
     
     //That's it you can test this code, This is working example.
  }
}


Note:- If you want to learn more about Action delegate, you can read here.
and here you can read parameterize Action<t> delegate.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900