Results 1 to 19 of 19

Thread: Invoking a Method with arguments? [Resolved]

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Invoking a Method with arguments? [Resolved]

    When calling InvokeMethod, one can simply do:

    VB Code:
    1. Dim p() As Object = {"your ugly mother", 0}
    2.         InvokeMethod(Me, "Button1_Click", p)


    Now, Button1_Click takes two arguments:

    sender of type System.Object,
    e of type System.EventArgs

    Which maps to where? Can someone explains what happens to "your ugly mother" and 0 when the Invoke method gets called?
    Last edited by mendhak; Nov 22nd, 2004 at 11:48 PM.

  2. #2
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036
    Hi

    I never use InvokeMethod to call a button_click inside another button_click and i do that a lot.

    Call Button1_Click(sender, e) i leave the call just for readabilility buts its not really necessary..

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by Asgorath


    I never use InvokeMethod to call a button_click inside another button_click and i do that a lot.


    Call Button1_Click(sender, e) i leave the call just for readabilility buts its not really necessary..
    I would do that, except I'm working to automate an app which has unknowns. Basically, any method could be invoked, so I need to work with this.

  4. #4
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    You should get a runtime error because the argument type have to match the method signature i.e.

    VB Code:
    1. Dim p() As Object = {Me, New MouseEventArgs( MouseButtons.Left,  _
    2. 1, _
    3. 1, _
    4. 1, _
    5. 0 ) }

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    MouseEventArgs would be applicable to all click events, am I correct?

  6. #6
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Yes, you are correct, anytime a mouse button is clicked, it generates a MouseClickEventArgs that is passed to the subsequent mouse click event.

    Have you considered using delegates with the invoke method?
    Whadayamean it doesn't work....
    It works fine on my machine!

  7. #7

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    I'm first trying to clear my delegate concepts.

    Are you talking about something like this?

    VB Code:
    1. Private Sub InvokeMethod(ByVal f As Form, ByVal MethodName As String, ByRef parms() As Object)
    2.         Dim eh As EventHandler = CType(System.Delegate.CreateDelegate(GetType(EventHandler), f, MethodName), EventHandler)
    3.  
    4.         If Not (eh Is Nothing) Then
    5.             f.Invoke(eh, parms)
    6.         End If
    7.  
    8.     End Sub

    ?

  8. #8
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Yes, you have that part correct, and actually, based on what I have read from these few posts, everything should work, is there a problem that I am not seeing?
    Whadayamean it doesn't work....
    It works fine on my machine!

  9. #9

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Yes, everything's working.

    I'm just looking to understand this better. In the form of detailed descriptions or better yet, useful, descriptive links.

  10. #10
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    ok, in your original post you listed some code and posed the question
    Which maps to where? Can someone explains what happens to "your ugly mother" and 0 when the Invoke method gets called?
    In your InvokeMethod method, you are taking the parms and passing them as arguments to the method you are calling in the f.Invoke method call.

    The .NET framework will attempt to use the parameters supplied to invoke the method you are calling with the MethodName parameter.

    For example, suppose you called "Button1_Click" as your named method, when the framework attempts to invoke that method, it is expecting arguments of types Object, and EventArgs, and the values you pass in must match that signature or the Invocation will fail.

    Because of the argument types you listed, "Your ugly mother" and 0 do not match the expected signature of Object and EventArgs (well actually just the EventArgs), that the method is calling, your call will fail.

    I hope that this is making sense, I am sort of rambling on.
    Whadayamean it doesn't work....
    It works fine on my machine!

  11. #11

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    That makes sense. So this means I can declare P to be

    VB Code:
    1. Dim p() As Object = {Me, New EventArgs}

    And pass it, and it'd work.

    Very good.

  12. #12
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    I think EventArgs is a MustInherit class...?

  13. #13

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Doesn't seem to be the case here... Framework 1.1, VS 2003.

  14. #14
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    You're right - it is not MustInherit, but...

    This class contains no event data; it is used by events that do not pass state information to an event handler when an event is raised. If the event handler requires state information, the application must derive a class from this class to hold the data.

  15. #15

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    In English, that would mean:

    If I have an event called StrangeThingsDone which accepts a lone string as an argument, when invoking the method handling it, I'd have to first inherit from EventArgs, (call it StrangeArgs), and then pass StrangeArgs to StrangeThings.

    I know I'm repeating what's being said, just need to make sure I have all that is correct, good and holy.

  16. #16
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Yes, you are correct, if you want to pass information such as a string to an event handler, then you should create a class "CustomEventArgs" that has a string property, instantiate a new instance of that class, populate the string property, and then pass it as the argument to your event. I have a lot of examples for this if you are interested.
    Whadayamean it doesn't work....
    It works fine on my machine!

  17. #17

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by CyberHawke
    I have a lot of examples for this if you are interested.
    Although I believe I have understood the concept, give me one or two examples. Also, where did you get "a lot of examples" from?

  18. #18
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    I wrote them, I do a lot of work with Remoting and the only way to pass arguments from a server to a client, over a Remoting channel is to create a custom event args class, make it serializable and pass it as an argument.
    Here are two examples of custom event args classes:
    VB Code:
    1. <Serializable()> _
    2.     Public Class CustomEventArgs
    3.         Inherits EventArgs
    4.         Protected _eventSource As String
    5.         Protected _pbCancel As Boolean
    6.  
    7.         Public Sub New(ByVal eventSource As String, ByVal cancel As Boolean)
    8.             _eventSource = eventSource
    9.             _pbCancel = cancel
    10.         End Sub
    11.  
    12.         Public ReadOnly Property EventSource() As String
    13.             Get
    14.                 Return _eventSource
    15.             End Get
    16.         End Property
    17.  
    18.         Public ReadOnly Property Cancel() As Boolean
    19.             Get
    20.                 Return _pbCancel
    21.             End Get
    22.         End Property
    23.     End Class

    and

    VB Code:
    1. <Serializable()> _
    2.     Public Class ProgressEventArgs
    3.         Protected _eventSource As String
    4.         Protected _progressDescription As String
    5.         Protected _percentComplete As Int16
    6.         Protected _progressCountLow As String
    7.         Protected _progressCountHigh As String
    8.  
    9.         Public Sub New(ByVal eventSource As String, ByVal progressDescription As String, ByVal percentComplete As Int16, ByVal progressCountLow As Int16, ByVal progressCountHigh As Int16)
    10.             _eventSource = eventSource
    11.             _progressDescription = progressDescription
    12.             _percentComplete = percentComplete
    13.             _progressCountLow = progressCountLow
    14.             _progressCountHigh = progressCountHigh
    15.         End Sub
    16.  
    17.         Public ReadOnly Property EventSource() As String
    18.             Get
    19.                 Return _eventSource
    20.             End Get
    21.         End Property
    22.  
    23.         Public ReadOnly Property ProgressDescription() As String
    24.             Get
    25.                 Return _progressDescription
    26.             End Get
    27.         End Property
    28.  
    29.         Public ReadOnly Property PercentComplete() As Int16
    30.             Get
    31.                 Return _percentComplete
    32.             End Get
    33.         End Property
    34.  
    35.         Public ReadOnly Property ProgressCountLow() As Int16
    36.             Get
    37.                 Return _progressCountLow
    38.             End Get
    39.         End Property
    40.  
    41.         Public ReadOnly Property ProgressCountHigh() As Int16
    42.             Get
    43.                 Return _progressCountHigh
    44.             End Get
    45.         End Property
    46.     End Class

    event declarations with the custom args:

    VB Code:
    1. Public Event Progress(ByVal sender As Object, ByVal args As ProgressEventArgs) Implements IJobServer.Progress
    2.         Public Event Start(ByVal sender As Object, ByVal args As CustomEventArgs) Implements IJobServer.Start

    Unfortunately, the project that I pulled these examples from use an invoke methodology instead of just raising the event, this is because multiple clients can subscribe to the events and if one dies, the server can test to see if it is still alive. If the server finds that a client has died, it can remove that clients subscription to the event. While this is cool code, it's not really applicable to your current situation and I'd rather not muck up what you are currently working on.
    Whadayamean it doesn't work....
    It works fine on my machine!

  19. #19

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    But that's good enough. Thanks for the examples,things are clearer now.

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