473,386 Members | 1,630 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,386 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 1599
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.