1

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!

0

3 Answers 3

3

Delegates are indeed what you need. In this case, since the operations all operate on a Car object and returns something, you need an Func<Car, Something>, where Something is the type that Horn and others return:

private void PerformOperation(Func<Car, Something> operation)
{
        var response = operation.Invoke(_car);
        if(response.Status != 0)
        {
            throw new Exception("Operation failed!");
        }
}

And you can write the two specialised operations like this:

public void SoundHorn()
{
    PerformOperation(car => car.Horn(1000));
}

public void TurnOnHazardLights()
{
    PerformOperation(car => car.ToggleHazards(true));
}
Sign up to request clarification or add additional context in comments.

3 Comments

Action<Car> has no return value. In that example you would need to define the parameter as Func<Car, int> to check for the result.
@PeterWurzinger oops! I didn’t notice that! But the return value doesn’t seem to be int though, seeing how there is a Status property.
@Sweeper Thanks a lot for the concise explanation here!
1

This would be

 private void PerformOperation(Func<int> operation)

And you Define

 public void TurnOnHazarLights() => PerformOperation(()=>_car.ToggleHazards(true));
 public void SoundHorn() => PerformOperation(() => m_car.SoundHorn(100))

Or replace < int > with the type of your Response class.

Comments

1

Maybe I'm overseeing something, but why wouldn't that work:

private void PerformOperation(Func<Response> operation)
{
            var response = operation();
            if(response.Status != 0)
            {
                throw new Exception("Operation failed!");
            }
}

You would then call it via

PerformOperation(() => _car.ToggleHazards(true));

2 Comments

Thanks! Indeed as I expected, this seems to be what I was looking for! Just a clarification, what would be the difference here if I used operation.Invoke() rather than just operation() as you have suggested?
Just syntactic sugar in this case - pick that version which looks better to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.