Joseph Devlin
Joseph Devlin

Reputation: 1800

Can I create a delegate in a class that will be passed into the constructor but not make it public?

I have a class like so.

public class Step<T>
{
    public delegate void Act<T>();

    Act<T> _act;

    public Step(Act<T> action)
    {
        Contract.Requires(action != null);

        _act = action;
    }

    public void PerformStep()
    {
        _act.Invoke();
    }
}

My problem is as follows. I want the constructor of this class to be able to accept a delegate with the definition you see in the class. What I do not want is for that delegate to be a public member of my class. The alternative of this is to have a public delegate outside of the class but no other class but this one will need it. Is there a way I can make the delegate private?

If that solution is not possible is there an alternative? This step class is just a wrapper for delegates that are to be run sequentially. There is no need for a signature if I can have a more generic solution.

EDIT: More info for clarification.

The aim of this class it to be able to allow the user to do something like the following.

If I have a Class Car. This car will have methods like Drive, Stop, Turn etc. Using this Step Class the user of the Class should be able to create an instance of their class then do the following.

Car c = new Car();
Step<Car> step1 = new Step<Car>(c.TurnLeft);
Step<Car> step2 = new Step<Car>(c.Drive);
Step<Car> step3 = new Step<Car>(c.TurnRight);
//add steps to a collection
c.AddNewRoutine(collection of steps);
c.RunRoutine(identify which routine);

The steps will be added to a data structure that will allow them to be executed in order. The car class will contain a data structure that can hold these groups of steps. This then allows any class to be created and have the user set up steps for that class to take, without the class or main program having to hardcode the behaviour.

To finish. I need the step class to be as it is. I just do not want to allow public access to that delegate in any form as the only class who needs it is the Step class.

Upvotes: 0

Views: 235

Answers (3)

Fede
Fede

Reputation: 44038

Remove the delegate alltogether and use an Action:

private Action<T> _action;

public Step(Action<T> action)
{
   _action = action;
}

Edit:

you can also have a simple System.Action with no type parameters

private Action _action;

public Step(Action action)
{
   _action = action;
}

However, keep in mind that when doing this:

var c = new Car();
var step1 = new Step(c.TurnLeft);

your're tying the step1 to the actual instance c, you will not be able to reuse that step for another instance:

var c2 = new Car(); //There's no way to call step1 on this =(

Upvotes: 1

p.s.w.g
p.s.w.g

Reputation: 149020

Your example uses a custom delegate that takes no parameters and has no return value. For this the System.Action may be ideal. If your custom delegate needs to supply a return value, use System.Func instead.

http://msdn.microsoft.com/en-us/library/system.action.aspx

http://msdn.microsoft.com/en-us/library/bb534960.aspx

From the sample you provided it looks like what you want is something more like this:

public class Step<T>
{
    Action _action;

    public Step(Action action)
    {
        Contract.Requires(action != null);

        _action = action;
    }

    public void PerformStep()
    {
        _action.Invoke();
    }
}

Upvotes: 2

itsme86
itsme86

Reputation: 19496

If other classes need to use the delegate then you need to make it public. Period.

Your only real option is to add a constructor that accepts some other kind of paramter(s) that the constructor can then turn into that delegate type.

Upvotes: 2

Related Questions