By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,573 Members | 1,912 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,573 IT Pros & Developers. It's quick & easy.

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

P: n/a
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(BufC ontrol.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! =)

------------
(*yes, i realize this code would never actually appear in the source file. -
i just list it as an example of what the final output 'should behave like')
Nov 20 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
hmm ok well i found what i thought i was looking for, "CallByName".

but how do i get it to work with AddHandler?

THANKS! =)

"K. Shier" <ks*****@spamAtYourOwnRisk.yahoo.com> wrote in message
news:eW**************@TK2MSFTNGP10.phx.gbl...
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(BufC ontrol.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! =)

------------
(*yes, i realize this code would never actually appear in the source file. - i just list it as an example of what the final output 'should behave like')

Nov 20 '05 #2

P: n/a
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
"K. Shier" <ks*****@spamAtYourOwnRisk.yahoo.com> wrote in message
news:eW**************@TK2MSFTNGP10.phx.gbl...
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(BufC ontrol.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! =)

------------
(*yes, i realize this code would never actually appear in the source file. - i just list it as an example of what the final output 'should behave like')

Nov 20 '05 #3

P: n/a
From what you have described you might want to look at reflection as a
possible solution.
"K. Shier" <ks*****@spamAtYourOwnRisk.yahoo.com> wrote in message
news:eW**************@TK2MSFTNGP10.phx.gbl...
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(BufC ontrol.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! =)

------------
(*yes, i realize this code would never actually appear in the source file. - i just list it as an example of what the final output 'should behave like')

Nov 20 '05 #4

P: n/a
* "K. Shier" <ks*****@spamAtYourOwnRisk.yahoo.com> scripsit:
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'.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #5

P: n/a
* "Tom Spink" <th**********@ntlworld.com> scripsit:
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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #6

P: n/a
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! =)
"K. Shier" <ks*****@spamAtYourOwnRisk.yahoo.com> wrote in message
news:eW**************@TK2MSFTNGP10.phx.gbl...
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(BufC ontrol.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! =)

------------
(*yes, i realize this code would never actually appear in the source file. - i just list it as an example of what the final output 'should behave like')

Nov 20 '05 #7

P: n/a
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
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bm************@ID-208219.news.uni-berlin.de...
* "Tom Spink" <th**********@ntlworld.com> scripsit:
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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #8

P: n/a
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" <ks*****@spamAtYourOwnRisk.yahoo.com> wrote in message
news:Ow**************@tk2msftngp13.phx.gbl...
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! =)
"K. Shier" <ks*****@spamAtYourOwnRisk.yahoo.com> wrote in message
news:eW**************@TK2MSFTNGP10.phx.gbl...
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(BufC ontrol.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! =)

------------
(*yes, i realize this code would never actually appear in the source

file. -
i just list it as an example of what the final output 'should behave

like')


Nov 20 '05 #9

P: n/a
* "Tom Spink" <th**********@ntlworld.com> scripsit:
I know.


I know that you know.

;-)

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #10

P: n/a
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
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bm************@ID-208219.news.uni-berlin.de...
* "Tom Spink" <th**********@ntlworld.com> scripsit:
I know.


I know that you know.

;-)

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #11

P: n/a
* "Tom Spink" <th**********@ntlworld.com> scripsit:
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

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #12

P: n/a
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
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bm************@ID-208219.news.uni-berlin.de...
* "Tom Spink" <th**********@ntlworld.com> scripsit:
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

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #13

P: n/a
* "Tom Spink" <th**********@ntlworld.com> scripsit:
And yet you keep posting replies... ;-)
You too...
EOT


EOT (really EOT).

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #14

P: n/a
very interesting... thanks 1,000,000! =) i will check it out today!

"Tom Spink" <th**********@ntlworld.com> wrote in message
news:ea**************@tk2msftngp13.phx.gbl...
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" <ks*****@spamAtYourOwnRisk.yahoo.com> wrote in message
news:Ow**************@tk2msftngp13.phx.gbl...
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! =)
"K. Shier" <ks*****@spamAtYourOwnRisk.yahoo.com> wrote in message
news:eW**************@TK2MSFTNGP10.phx.gbl...
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(BufC ontrol.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! =)

------------
(*yes, i realize this code would never actually appear in the source

file. -
i just list it as an example of what the final output 'should behave

like')



Nov 20 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.