How do I get the result of a method called via delegate handler?
The following code is just a homework task, therefore delegate
there may look totally meaningless
So, I have a Hangman game class and it has a method that reads game data from a file. If that file is not found - a method from user code should be called:
catch (FileNotFoundException)
{
exceptionOnLoad?.Invoke("File doesn't exist");
}
So there I have a delegate and a handler method, that assigns a custom user method to it:
public delegate bool ExceptionErrorHandler(string message);
private ExceptionErrorHandler? exceptionOnLoad;
public void RegisterExceptionErrorHandler(ExceptionErrorHandler method)
{
exceptionOnLoad = method;
}
So here's a method from user code that exceptionOnLoad
is supposed to call:
static bool HandleOnErrorMessage(string message)
{
Console.WriteLine(message);
return true;
}
It returns bool, because I'd like to use it as an indicator for stopping a Hangman game, but it is called via the following construction and there's nowhere to write that bool value to:
hangmanGame.RegisterExceptionErrorHandler(HandleOnErrorMessage);
Is there a way to store the bool-result of HandleOnErrorMessage
in a variable? Or maybe there's a better way to use delegate
and achieve desirable result in this situation?
I was thinking about this:
catch (FileNotFoundException)
{
bool? load = exceptionOnLoad?.Invoke("File doesn't exist");
}
And then making a file loading method also output this bool, but this seems strange (maybe in vain).
// you declared the delegate
public delegate bool ExceptionErrorHandler(string message);
// if you want to return the output of the delegate method, it needs to return
// same type as such delegate
public bool RegisterExceptionErrorHandler(ExceptionErrorHandler method)
{
const string message = "Call from RegisterExceptionErrorHandler";
if (method != null) // can be null, yes
return method(message);
else
return false;
}
// USAGE
private void DoSomething(SomeEnum val)
{
bool retval;
if (val == SomeEnum.Val1) // Based on enum value
retval = RegisterExceptionErrorHandler(Method1); // you invoke one method or another
else if (val == SomeEnum.Val2)
retval = RegisterExceptionErrorHandler(Method2);
else
retval = RegisterExceptionErrorHandler(Method3);
if (retval)
RunSomeCode();
}
Methods Method1
, Method2
and Method3
must be declared with the signature of the delegate
- bool X(string)
If it's for homework i would just .invoke() it like a simple method, as you described.
It's not a weird solution to be honest, the only weird thing here is the homework itself. I don't see any point on doing this other than learning about delegates, of course.
Here's a working example in a console application:
// We define the delegate
public delegate bool Handler(string message);
// We assign a handler method with a lambda
public static Handler SomeHandler => HandlerMethod;
public static bool HandlerMethod(string message)
{
// Do what you need with the message and return a value
return true;
}
static void Main(string[] args)
{
// We call it and store the value in a variable!
var variable = SomeHandler.Invoke("Some message");
}