I suppose this is a fairly simple thing that I am trying to do but I am probably missing something here.
Say I have a few functions that all do ALMOST the same thing except for one line, this definitely calls for a "base" function or something along those lines, where that ONE different line would be passed over to it and executed, and the rest of the code will continue it's flow.
Here's my example:
Let's say I have a few functions that perform operation on a Car object and validate that the operation is successful:
public void SoundHorn()
{
var response = _car.Horn(1000);
if(response.Status != 0)
{
throw new Exception("Operation failed!");
}
}
public void TurnOnHazardLights()
{
var response = _car.ToggleHazards(true);
if(response.Status != 0)
{
throw new Exception("Operation failed!");
}
}
As you can see the only difference is the actual functionto perform, the validation is the same for all of them.
What I ultimately would want is to have a "base" function along those lines:
private void PerformOperation(??? operation)
{
var response = operation.Invoke();
if(response.Status != 0)
{
throw new Exception("Operation failed!");
}
}
And so the other functions would look like:
public void TurnOnHazarLights()
{
PerformOperation(_car.togglehazards);
}
I am aware of Delegates, Actions and Func, and it seems that they require me to pass over the Function to run itself, without it's object reference, and this I unable to use the above syntax.
I suppose I am missing the usage of one of these above mentioned classes.
Could you please enlighten me on this?
Thank you!