Click to See Complete Forum and Search --> : A delegates issue


Hada
April 30th, 2005, 11:28 AM
I have a delegate in C#, but when I try to invoke the method it represents, I came up with this problem, in the MSDN I found this.

public object DynamicInvoke(
object[] args
);

And this is the code I have:
****************************************
public delegate ProductoT P_BC(string a, string b);

P_BC PF_BC = new P_BC(Producto.BuscarCodigoS);

PF_BC.DynamicInvoke("have no idea");
****************************************

The function "BuscarCodigoS" needs to get two string arguments, and return a ProductoT object, just like the delegate shows, but when I try to invoke the function it asks me for "object[] args", which I have no idea how of what is, does it means that I need to create a class for the arguments? do you have an example of this?

have no idea = object[] args.

MadHatter
April 30th, 2005, 12:44 PM
the function requires an array of objects. since object is the base class for everything, you can pass in any type of array. it requires this because your implementing function can have any number of parameters. by requiring an array of objects, it places the developer responsible for ensuring that they # of items in the array, match quantity and order of the parameters required in the function.

PF_BC.DynamicInvoke(new string[] {"string 1", "string 2"});

Hada
May 1st, 2005, 02:44 PM
Hey thanks !, I already figured out how to do it, I did not know what the object type was, now I know that object is a generic type, then what I need is just to create an object array before I send it as argument into the DynamicInvoke method, thanks :)

MadHatter
May 1st, 2005, 06:53 PM
it doesnt need to be an "object[]" it can be any type of array. it all depends on what you have as parameters. if your parameters are all string, you can do a string array, if your parameters are all custom objects, you can make a custom objects array. if you have mixed parameters (as is typically the case), then you can make the object array and insert whatever types of objects in that array.

FoodBard
May 2nd, 2005, 10:10 PM
BTW, I've seen much code that does

Handler.Invoke(obj, arg)

Instead of

Handler(obj, arg)

Is there a difference?