473,326 Members | 2,104 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,326 software developers and data experts.

Dynamically call a function depending on the value of a variable

I am using a treeview and within each node, I want to store the name of the
function that was called to create the node's data.

Then whenever a user selects this node, I want to be able to call that
function without having to create a huge SELECT CASE statement.

i.e.

Node.123's data is created in a function called "fnGetProjectDetails"

I want to be able to use say the node.tag field to store "fnGetProjectDetails"

and then when that node is selected to be able to simply call node.tag to
refresh the data.

I am still a rookie with .NET, so please go easy with me !

Is this possible?
Jul 21 '05 #1
3 1594
Rather than storing the text name of the function, store a delegate instead.

A delegate is a type that refers to a method. The EventHandler delegate is
probably the best known (for Win Form developers at least), although you will
probably not be aware you are using them.

Google/MSDN can probably provide much better guidance for you then I can,
but hopefully this will point you in the right direction.

Dan

"Supa Hoopsa" wrote:
I am using a treeview and within each node, I want to store the name of the
function that was called to create the node's data.

Then whenever a user selects this node, I want to be able to call that
function without having to create a huge SELECT CASE statement.

i.e.

Node.123's data is created in a function called "fnGetProjectDetails"

I want to be able to use say the node.tag field to store "fnGetProjectDetails"

and then when that node is selected to be able to simply call node.tag to
refresh the data.

I am still a rookie with .NET, so please go easy with me !

Is this possible?

Jul 21 '05 #2
You did not say what language you are using but here is an example in C# of
how to do what you would like to do. You can call any method of any class
using reflection.

// create an instance of the class containing the function to execute
Foo myClass = new Foo();

// get the class type
Type myType = myClass.GetType();

// get the method you want to invoke ("myMethod" can be a string variable)
System.Reflection.MethodInfo myMethod = t.GetMethod("myMethod");

// create an object array with the parameter values for the function call
// if these are variable there are more reflection methods to get the list of
// of parameters for the function
object[] myParams = new object["Param1", "ParamEct"];

// invoke the method
object retValue = myMethod.Invoke(myClass, myParams);

I hope this helps.

Greg

"Supa Hoopsa" wrote:
I am using a treeview and within each node, I want to store the name of the
function that was called to create the node's data.

Then whenever a user selects this node, I want to be able to call that
function without having to create a huge SELECT CASE statement.

i.e.

Node.123's data is created in a function called "fnGetProjectDetails"

I want to be able to use say the node.tag field to store "fnGetProjectDetails"

and then when that node is selected to be able to simply call node.tag to
refresh the data.

I am still a rookie with .NET, so please go easy with me !

Is this possible?

Jul 21 '05 #3
Sorry. The one line in the example has a typo;

Was:
// get the method you want to invoke ("myMethod" can be a string variable)
System.Reflection.MethodInfo myMethod = t.GetMethod("myMethod");

Should Be:
// get the method you want to invoke ("myMethod" can be a string variable)
System.Reflection.MethodInfo myMethod = myType.GetMethod("myMethod");

Greg

"Supa Hoopsa" wrote:
I am using a treeview and within each node, I want to store the name of the
function that was called to create the node's data.

Then whenever a user selects this node, I want to be able to call that
function without having to create a huge SELECT CASE statement.

i.e.

Node.123's data is created in a function called "fnGetProjectDetails"

I want to be able to use say the node.tag field to store "fnGetProjectDetails"

and then when that node is selected to be able to simply call node.tag to
refresh the data.

I am still a rookie with .NET, so please go easy with me !

Is this possible?

Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
2
by: Joe | last post by:
I have 3 functions: ClientInfoA is doing something ClientInfoB is doing something SelectFunction2Run is a function to determine which function needed to run based on the value of the variable...
8
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
0
by: theo | last post by:
Hello..I have a similar issue as yesterday but the situation has slightly changed so perhaps someone can shed some more light on the issue... Program flow - load file,extract xml text tags from...
3
by: Supa Hoopsa | last post by:
I am using a treeview and within each node, I want to store the name of the function that was called to create the node's data. Then whenever a user selects this node, I want to be able to call...
7
by: Steve_Black | last post by:
Hello, I'm toying with the idea of loading a MenuStrip (VB.Net 2005) dynamically based on who is logged into my system. Every user has different security settings and I want to customize the...
5
by: sfeher | last post by:
Hi All, I need to call a function(loaded with appendChild) for which I have the name as a string. .... var fnName = 'fn1'; var call = fnName + '('+ param +' )'; eval(call);
7
by: Ron Goral | last post by:
Hello I am new to creating objects in javascript, so please no flames about my coding style. =) I am trying to create an object that will represent a "div" element as a menu. I have written...
9
by: Dahak | last post by:
I'm trying to generate dynamic functions to use as separate callbacks for an AJAX API call. The API doesn't seem to allow for the inclusion of any parameters in the callback, so I can't...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.