Reputation: 11330
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.
Upvotes: 1
Views: 179
Reputation: 1277
Try this:
if (!(DoThis() && DoThat()))
then error;
if (error)
FailoverActions();
Upvotes: 0
Reputation: 14672
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.
Upvotes: 0
Reputation: 138017
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();
Upvotes: 1
Reputation: 6401
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()
{
}
}
Upvotes: 1
Reputation: 48568
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();
Upvotes: 2
Reputation: 13925
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.
Upvotes: 0
Reputation: 116674
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
}
}
Upvotes: 1