1

I have an idea that sounds like it might work but I'm not entirely sure, so looking for advice as to whether this can be achieved and how.

On my web form, i have a bool value named 'error'.

There are a number of things that need to happen on a page for it to be successfully loaded.

I could write code like this:

bool thisSuccess = DoThis();
if(!thisSuccess)
    then error;

bool thatSuccess = DoThat();
if(!thatSuccess)
    then error;

if(error)
  FailoverActions();

and so on.

Of course that would be wholley inefficient so I thought it may be posible to create a delegate of some kind where the code would look something like this:

error = DoThis();

... and some kind of trigger here that called a function when error = true;

Apologies for the lack of precise detail but this is new ground for me.


Update

Thanks to everyone for their great ideas.

The reason that there's little detail is that i'm very inexperienced and what I've found to date with .net is that although there are many ways to crack an egg, there's generally some better than others.

I appreciate your experienced views.

Thanks again.

4
  • 1
    You are going to get 5 answers telling you to use Exceptions. As a preemptive strike, can you explain why are you not using exceptions here?
    – Kobi
    Sep 11, 2012 at 10:10
  • "Of course that would be wholley inefficient" - Why? Sep 11, 2012 at 10:10
  • @kobi - although an error has occurred, I might want to handle the exception such as a redirect to a specific alternative page.
    – dotnetnoob
    Sep 11, 2012 at 10:30
  • @user1437135 - I'm not sure I follow... That is why you have catch blocks - you can have one for each error type. Again, exceptions are not recommended for common issues (validation, logic or general program flow), but are very recommended for errors.
    – Kobi
    Sep 11, 2012 at 10:32

7 Answers 7

2

Why not. A method that returns bool value assigned to a delegete.

This way

public delegate bool PerformCalculation();

PerformCalculation = DoThis();
if (!PerformCalculation())
    then error;

PerformCalculation = DoThat();
if(!PerformCalculation())
    then error;

if(error)
    FailoverActions();

Alternate Solution

No need for delegate. Simply have 2 methods

bool DoThis() and bool DoThat()

if (!DoThis())
    then error;

if(!DoThat())
    then error;

if(error)
    FailoverActions();
2
  • 2
    Is this an answer? I agree that the question is not very clear but you could have at least provided some example. Sep 11, 2012 at 10:08
  • A delegate is unnecessary in this case. Why not just do if(DoThis()) .. else if(DoThat()) .. else .. Sep 11, 2012 at 10:09
1

First - having your methods return true or false is a questionable practice - it looks like you should be using exceptions to handle this, especially if errors are relatively rare.

Sample code:

try
{
    DoThis();
    DoThat();
}
catch(DoingThingsException ex)
{
    FailoverActions();
    //throw; //?
}

As for a quick solution, one option is short-circuiting:

bool success = DoThis() && DoThat() && DoTheOther();
if(!success) FailoverActions();
1
  • I decided that the right approach was to throw an exception in this case, so have given Kobi the answer as he was first to recommend in original comments.
    – dotnetnoob
    Sep 12, 2012 at 9:22
1

You could use Func<bool> to represent an initialization step:

var steps = new List<Func<bool>>()
{
    Step1,
    Step2,
    Step3
};

Where Step1, etc. are methods returning bool.

Then this loop calls them:

foreach (var step in steps)
{
    if (!step())
    {
        // an error occurred

        break; // use break to exit if necessary
    }
}
2
  • Instead of the loop, you can also do bool success = steps.All(step => step());. Not sure this is a useful comment.
    – Kobi
    Sep 11, 2012 at 10:23
  • Yes, or .Any(step => !step()) to quit on the first failure. Sep 11, 2012 at 10:32
1

How about something like:

public class MyClass
{
    private bool _error;

    private Func<bool> DoThis;
    private Func<bool> DoThat;

    public MyClass()
    {
        DoThis = () => true;
        DoThat = () => false;

        Validate();
    }

    public void Validate()
    {
        Error = DoThis() && DoThat();
    }

    public bool Error
    {
        get { return _error;  }
        set { 
            _error = value;
            if (_error) FailoverActions();
        }
    }

    public void FailoverActions()
    {

    }
}
0

Delegates have unspecified behavior regarding return value. For example if multiple handlers are assigned to a delegate one of the return values used, but you have no control over which one. This may lead cases where successful function overshadows some error. Not to mention the danger of calling unassigned delegates.

There are better options, but you should clarify what you want to achieve.

1
  • 1
    If everyone took that concern seriously, there would be no such thing as Linq: it depends on delegates returning values. Sep 11, 2012 at 10:14
0

What you are describing would suit a state machine designe pattern.
This kind of thing can be modeled using Windows Workflow, which actually includes state machine workflows in the latest version.

0

Try this:

if (!(DoThis() && DoThat()))
  then error;

if (error)
  FailoverActions();

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.