473,395 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
14 1635
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
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
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
* "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
* "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
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
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
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
* "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
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
* "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
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
* "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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: damian birchler | last post by:
Hello there! I'm wondering if it is possible to automatically dynamically add methods to a class instance. For example, if there is a class Foo with one method named add_function and an...
8
by: Kevin Little | last post by:
#!/usr/bin/env python ''' I want to dynamically add or replace bound methods in a class. I want the modifications to be immediately effective across all instances, whether created before or...
1
by: Jeff Smith | last post by:
Can I load custom web user controls dynamically and access the properties and methods without having to explicitly define custom control types (example 2 below). I have custom web control named...
5
by: SStory | last post by:
Hi all, I really needed to get the icons associated with each file that I want to show in a listview. I used the follow modified code sniplets found on the internet. I have left in...
12
by: leaf | last post by:
Hi, How to call function at runtime, based on a struct that contains the information for the function call: struct func_to_call { int function_id; // function id to call unsigned int nparams;...
3
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user...
9
by: Allen | last post by:
The arguments of function is variable. Given function address, argument type and data, how to dynamically call the function? The following is pseudo code. int count = 0; int offset = 0; char...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
3
by: johanatan | last post by:
When I first heard about these new features, I was very excited as it would have (if implemented as I had expected) rendered mimicking multiple inheritance almost painless in C#. Unfortunately,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.