CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2005
    Location
    Juarez, México
    Posts
    30

    Smile A delegates issue

    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.

  2. #2
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: A delegates issue

    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.

    Code:
    PF_BC.DynamicInvoke(new string[] {"string 1", "string 2"});
    Last edited by MadHatter; April 30th, 2005 at 12:46 PM.

  3. #3
    Join Date
    Apr 2005
    Location
    Juarez, México
    Posts
    30

    Thumbs up Re: A delegates issue

    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

  4. #4
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: A delegates issue

    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.

  5. #5
    Join Date
    Apr 2005
    Posts
    298

    Re: A delegates issue

    BTW, I've seen much code that does

    Code:
    Handler.Invoke(obj, arg)
    Instead of

    Code:
    Handler(obj, arg)
    Is there a difference?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured