call methods via a string name (dynamically) at runtime?

  • Thread starter Thread starter K. Shier
  • Start date Start date
K

K. Shier

i need to iterate through a collection of controls and set event handlers
using the given control's name as a basis. in pseudocode
(wishful-thinkingcode!):

AddHandler BufControl.validateevent, AddressOf
TheMagicalFunctionThatTurnsAStringIntoAMethod(BufControl.Name & "_Validate")

assume BufControl = txtAdd1, the substituted output i am looking for is*:

AddHandler txtAdd1.ValidateEvent, AddressOf txtAdd1_Validate

what is the real name of 'TheMagicalFunction...', please? i just saw a post
on this the other day but i have no chance of finding it as i don't know any
legitimate keywords!

THANKS! =)
 
hmm ok well i found what i thought i was looking for, "CallByName".

but how do i get it to work with AddHandler?

THANKS! =)
 
Hi, I think you're referring to the CallByName method, which is not exactly
what you want.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
From what you have described you might want to look at reflection as a
possible solution.
 
* "Tom Spink said:
Hi, I think you're referring to the CallByName method, which is not exactly
what you want.

ACK. 'CallByName' doesn't help in this situation.
 
* "K. Shier said:
hmm ok well i found what i thought i was looking for, "CallByName".

but how do i get it to work with AddHandler?

It doesn't work with 'CallByName'.
 
thanks for the responses!

Tom: i knew the "System.Reflection Master" wouldn't be able to resist this
thread... ;-)

anybody feel like giving the quick answer? otherwise, i *will* pore through
the entirety of system.reflection this afternoon in order to find it myself,
but i really don't have the time... ;-)

THANKS! =)
 
I know.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
Hi, I *love* reflection.

To answer your question, it looks like reflection is what you're after, try
this:

Private Sub AddHandlerString(ByVal DestObj As Object, ByVal DestEvnt As
String, ByVal clsClass As Object, ByVal strMethod As String)

Dim dlgEvent As [Delegate] =
[Delegate].CreateDelegate(GetType(EventHandler), clsClass, strMethod)
Dim eiEvent As Reflection.EventInfo = DestObj.GetType.GetEvent(DestEvnt)

eiEvent.AddEventHandler(Me, dlgEvent)

End Sub

Now, use it like this:

AddHandlerString(txtAdd1, "ValidateEvent", Me, BufControl.Name &
"_Validate")

Note: This code is untested.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
Now you know that I know that you know I know.

This thread could go on for a while, so EOT.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
* "Tom Spink said:
Now you know that I know that you know I know.

This thread could go on for a while, so EOT.

Yep. We do not want threads with length +inf here.

EOT
 
And yet you keep posting replies... ;-)

EOT

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
very interesting... thanks 1,000,000! =) i will check it out today!

Tom Spink said:
Hi, I *love* reflection.

To answer your question, it looks like reflection is what you're after, try
this:

Private Sub AddHandlerString(ByVal DestObj As Object, ByVal DestEvnt As
String, ByVal clsClass As Object, ByVal strMethod As String)

Dim dlgEvent As [Delegate] =
[Delegate].CreateDelegate(GetType(EventHandler), clsClass, strMethod)
Dim eiEvent As Reflection.EventInfo = DestObj.GetType.GetEvent(DestEvnt)

eiEvent.AddEventHandler(Me, dlgEvent)

End Sub

Now, use it like this:

AddHandlerString(txtAdd1, "ValidateEvent", Me, BufControl.Name &
"_Validate")

Note: This code is untested.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations


K. Shier said:
thanks for the responses!

Tom: i knew the "System.Reflection Master" wouldn't be able to resist this
thread... ;-)

anybody feel like giving the quick answer? otherwise, i *will* pore through
the entirety of system.reflection this afternoon in order to find it myself,
but i really don't have the time... ;-)

THANKS! =)


a
post
know
 
Back
Top