1

I am trying to declare a action-delegate which allows set functions with different paremeter.

Here is my minimal example: I have different void function like this:

private void Function1(string Parameter) { ... } private void Function2(int Parameter) { ... } 

I want to pass these functions in the same constructor. But this is not possible in this way.

Main { AClass a = new AClass(Function1); AClass b = new AClass(Function2); } 

How do I need to define my Action (object is not working) - I don't want to overload my consturcor!

public class AClass { public AClass(Action<???> Function) { ... } } 

Thank you for helping me!

2 Answers 2

2

Typically you would either do this:

public class AClass<T> { public AClass(Action<T> Function) { ... } } 

...or you would overload the constructor.

If you have something else in mind you need to provide more code to show us what you want to do.

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

Comments

1

You could make your class generic:

public class AClass<T> { private Action<T> _action; public AClass(Action<T> function) { _action = function; } public void Exec(T parameter) { _action(parameter); } } 

private void MyMethod(int param) { // .... } var myClass = new AClass<int>(MyMethod); myClass.Exec(10); 

or

private void MyMethod(string param) { // .... } var myClass = new AClass<string>(MyMethod); myClass.Exec("Hello world!"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.