|
-
Nov 22nd, 2004, 06:28 AM
#1
Invoking a Method with arguments? [Resolved]
When calling InvokeMethod, one can simply do:
VB Code:
Dim p() As Object = {"your ugly mother", 0}
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.
-
Nov 22nd, 2004, 07:15 AM
#2
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."
-
Nov 22nd, 2004, 07:27 AM
#3
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.
-
Nov 22nd, 2004, 07:36 AM
#4
You should get a runtime error because the argument type have to match the method signature i.e.
VB Code:
Dim p() As Object = {Me, New MouseEventArgs( MouseButtons.Left, _
1, _
1, _
1, _
0 ) }
-
Nov 22nd, 2004, 07:50 AM
#5
MouseEventArgs would be applicable to all click events, am I correct?
-
Nov 22nd, 2004, 08:11 AM
#6
Hyperactive Member
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!

-
Nov 22nd, 2004, 08:15 AM
#7
I'm first trying to clear my delegate concepts.
Are you talking about something like this?
VB Code:
Private Sub InvokeMethod(ByVal f As Form, ByVal MethodName As String, ByRef parms() As Object)
Dim eh As EventHandler = CType(System.Delegate.CreateDelegate(GetType(EventHandler), f, MethodName), EventHandler)
If Not (eh Is Nothing) Then
f.Invoke(eh, parms)
End If
End Sub
?
-
Nov 22nd, 2004, 09:00 AM
#8
Hyperactive Member
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!

-
Nov 22nd, 2004, 12:07 PM
#9
Yes, everything's working. 
I'm just looking to understand this better. In the form of detailed descriptions or better yet, useful, descriptive links.
-
Nov 22nd, 2004, 12:28 PM
#10
Hyperactive Member
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!

-
Nov 22nd, 2004, 11:44 PM
#11
That makes sense. So this means I can declare P to be
VB Code:
Dim p() As Object = {Me, New EventArgs}
And pass it, and it'd work.
Very good.
-
Nov 23rd, 2004, 03:15 AM
#12
I think EventArgs is a MustInherit class...?
-
Nov 23rd, 2004, 03:43 AM
#13
Doesn't seem to be the case here... Framework 1.1, VS 2003.
-
Nov 23rd, 2004, 04:41 AM
#14
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.
-
Nov 23rd, 2004, 04:48 AM
#15
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.
-
Nov 23rd, 2004, 07:13 AM
#16
Hyperactive Member
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!

-
Nov 23rd, 2004, 07:15 AM
#17
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?
-
Nov 23rd, 2004, 07:28 AM
#18
Hyperactive Member
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:
<Serializable()> _
Public Class CustomEventArgs
Inherits EventArgs
Protected _eventSource As String
Protected _pbCancel As Boolean
Public Sub New(ByVal eventSource As String, ByVal cancel As Boolean)
_eventSource = eventSource
_pbCancel = cancel
End Sub
Public ReadOnly Property EventSource() As String
Get
Return _eventSource
End Get
End Property
Public ReadOnly Property Cancel() As Boolean
Get
Return _pbCancel
End Get
End Property
End Class
and
VB Code:
<Serializable()> _
Public Class ProgressEventArgs
Protected _eventSource As String
Protected _progressDescription As String
Protected _percentComplete As Int16
Protected _progressCountLow As String
Protected _progressCountHigh As String
Public Sub New(ByVal eventSource As String, ByVal progressDescription As String, ByVal percentComplete As Int16, ByVal progressCountLow As Int16, ByVal progressCountHigh As Int16)
_eventSource = eventSource
_progressDescription = progressDescription
_percentComplete = percentComplete
_progressCountLow = progressCountLow
_progressCountHigh = progressCountHigh
End Sub
Public ReadOnly Property EventSource() As String
Get
Return _eventSource
End Get
End Property
Public ReadOnly Property ProgressDescription() As String
Get
Return _progressDescription
End Get
End Property
Public ReadOnly Property PercentComplete() As Int16
Get
Return _percentComplete
End Get
End Property
Public ReadOnly Property ProgressCountLow() As Int16
Get
Return _progressCountLow
End Get
End Property
Public ReadOnly Property ProgressCountHigh() As Int16
Get
Return _progressCountHigh
End Get
End Property
End Class
event declarations with the custom args:
VB Code:
Public Event Progress(ByVal sender As Object, ByVal args As ProgressEventArgs) Implements IJobServer.Progress
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!

-
Nov 24th, 2004, 01:56 AM
#19
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|