I am probably just being thick but I cannot figure out what to do here….
I have a library I am trying to create that will have a virtual function that the user can use to create a custom error handler to call later in the code. One of the main hiccups I am hitting is that these errors will be stored in a collection for calling randomly.
Here’s some pseudo code
class Error
{
string Error {get; set;}
int ErrorCode {get; set;}
public void ErrorFunction(params obj[] arrParams);
}
class program
{
List<Error> errorlist = new List<Error>();
static void Main(string[] args)
{
initList();
errorlist[0].ErrorFunction("Sometext");
}
public void initList()
{
Error err = new Error();
err.ErrorFunction = MyOverloadedError;
errorlist.add(err);
}
public void MyOverloadedError(params obj[] arrObjects)
{
Console.WriteLine(arrObjects);
}
}
I seem to remember being able to do this simply with a
void* and a call using the addressof for assigning the function to be called.
Like I said perhaps I am being thick, I know that I have to use a delegate, but I cannot figure out how to define a delegate where it exists in an instance of the class and not as a static member of the class.
to reiterate (since I am tired and not sure if I am making this clear or just more confusing) I want to make a member function that can have a custom function assigned to it.
so basically I can do the following
Error myError = new Error();
myError.ErrorFunction = SomeCustomFunction;
...
myError.ErrorFunction("Some Parameters"); //effectively calling some static function elsewhere
Thanks in advance! my brain hurts too much….
Not sure what you want to achieve or whether an event is more appropriate, but here’s your pseude-code, fixed up to be valid, working C# code: