473,770 Members | 2,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a function dynamically

Joe
I have 3 functions: ClientInfoA is doing something
ClientInfoB is doing something

SelectFunction2 Run is a function to determine which function needed to
run based on the value of the variable Method2Run. If the clientType
is A, it would run ClientInfoA function. If it is clientType B, it
would run the ClientInfoB function. Based on the value of Method2Run,
how would I run the function dynamically? I know that there are many
ways not to do this. But I am creating this for easy maintenance.

Thanks all


function ClientInfoA ()
{
....
....
}

function ClientInfoB ()
{
....
....
}

Function SelectFunction2 Run
var Method2Run
switch (clientType)
{
case "A": Method2Run = ClientInfoA
break;
case "B": Method2Run = ClientInfoB
break
}
// how do I run the Method2Run????
Run Method2Run (whatever the value)
Jul 23 '05 #1
2 1832
Ivo
"Joe" wrote
I have 3 functions: ClientInfoA is doing something
ClientInfoB is doing something

SelectFunction2 Run is a function to determine which function needed to
run based on the value of the variable Method2Run. If the clientType
is A, it would run ClientInfoA function. If it is clientType B, it
would run the ClientInfoB function. Based on the value of Method2Run,
how would I run the function dynamically? I know that there are many
ways not to do this. But I am creating this for easy maintenance.

function ClientInfoA () {...}

function ClientInfoB () {...}

Function SelectFunction2 Run
Strange line, this. A capital F, no brackets, no accolades...
var Method2Run
switch (clientType)
{
case "A": Method2Run = ClientInfoA
break;
case "B": Method2Run = ClientInfoB
break
}
// how do I run the Method2Run????
Run Method2Run (whatever the value)


According to this, the choice is made based on the value of a global
variable "clientType " which presumably gets set elsewhere. Then there is no
need for an extra variable "Method2Run ". See this:

function SelectFunction2 Run (){
if(clientType== 'A') ClientInfoA(); else
if(clientType== 'B') ClientInfoB(); else
alert('Sorry, could not determine the method to run.');
}

There are other ways. Some would use eval().
HTH
Ivo
Jul 23 '05 #2
Joe wrote:
I have 3 functions: ClientInfoA is doing something
ClientInfoB is doing something

SelectFunction2 Run is a function to determine which function needed
to run based on the value of the variable Method2Run. If the
clientType is A, it would run ClientInfoA function. If it is
clientType B, it would run the ClientInfoB function.
Only 2 types of client?
Based on the value of Method2Run,
how would I run the function dynamically? I know that there are many
ways not to do this. But I am creating this for easy maintenance. <snip> function ClientInfoA ()
{
....
}

function ClientInfoB ()
{
....
}

Function SelectFunction2 Run ^ ^^
An upper case - F - will not help. An arguments/parameters list and the
opening brace would probably also be a good idea.
var Method2Run
switch (clientType)
{
case "A": Method2Run = ClientInfoA
break;
case "B": Method2Run = ClientInfoB
break
}
Not having a -default: - in a switch statement is asking for trouble.
// how do I run the Method2Run????
Run Method2Run (whatever the value)


The literal answer would be:-

function SelectFunction2 Run(){
var Method2Run
switch (clientType){
case "A": Method2Run = ClientInfoA;
break;
default: Method2Run = ClientInfoB;
break;
}
Method2Run();
}

But since - Method2Run - is a local variable and will go out of scope as
soon as the function exits you may as well just call one of the two
functions directly based on the - clientType - variable:-

function SelectFunction2 Run(){
if(clientType == "A"){
ClientInfoA();
}else{
ClientInfoB();
}
}

- or slightly more compactly:-

function SelectFunction2 Run(){
((clientType == "A")?ClientInfo A:ClientInfoB)( );
}

But that leaves you calling - SelectFunction2 Run - whenever you wanted
to call which ever of the two functions was to be used, and repeating
the test on each call. A more interesting approach is to define the
whole lot as one function, call that function each time you wanted the
facility but have the function re-configure itself on the first occasion
it was called. Thus subsequent testing is not necessary and code using
the function exclusively uses one identifier and has no idea that the
function re-configured itself the first time it was called (I am
assuming that - clientType - does not change while the script is
running):-

function callThisFunctio n(){
function ClInfoA(){
...
}
function ClInfoB(){
...
}
/* Replace this function with one of its two inner functions
so subsequent calls will execute that function directly
instead. And call whichever function is assigned as the
replacement so it can act for this initial function call:-
*/
(this.callThisF unction = ((clientType==" A")?ClInfoA:ClI nfoB))();
}

Apart from the global - clientType - variable (which is almost certainly
a mistake and should be replaced with feature detection directly related
to the choice being made at this point), the result is self-contained,
easily portable and outwardly simple. All of which contribute towards
easy maintenance.

Richard.
Jul 23 '05 #3

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

Similar topics

4
2433
by: jarmopy | last post by:
Hi, I have made a service with C# and calling that service class from another C# program with remoting. (Referendes from the calling program) That service class is configured so that garpage collection is not used in this class. (singleton class + override InitializeLifetimeService ) The service class uses C++ unmanaged function from dll (using DLLimport).
4
7292
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the constructor again. In my larger program, I find that the member variables don't get re-initialized when "reconstructed". I don't have that problem in this demo, but the second time the constructor is called, its "this" points to a different location. ...
5
3034
by: Francesco Bochicchio | last post by:
Hi all, anybody knows if there is a (standard, portable) way to dinamically build a list of parameters to call a C function? Something like va_start & co, but to be used on the calling side? In other words, suppose that I have a generic pointer : void * f_ptr; I know that this pointer points to a fuction. I also know the function
5
2464
by: Simon Harris | last post by:
Hi All, I am trying to call a method in a web form, from an event fired in a user control. My user control displays a map, which has a link button to enlarge/shrink the map. When the user enlarges the map, I want to hide my navigation table etc, maximising the viewing area. I've been working on this for 5 hours now, so far I have as detailed below - Which rund with out error, but the final function never gets called. Any help/suggestions...
35
2895
by: Michel Sanner | last post by:
Hello, One of the greatest feature of Python in my opinion is the way the interpreter can be used to integrate a wide variety of software packages by dynamically linking them. This approach has been extremely successful for us so far but now I run into a license nightmare. Some the libraries we wrapped using SWIG are under GPL but the applications we are distributing are not (mainly because
3
1383
by: hardieca | last post by:
My apologies if this gets posted twice... I am trying to determine whether or not a class has a function, and if it does, build a reference to it dynamically which I can then use to access the original function's functionality. So, for the first part, I'm looking for some way to do the following: if (hasMember(object o, string member){ //Processing
5
2221
by: sfeher | last post by:
Hi, Is there a way to know when a function is available for me to call it from a dynamically loaded a javascript? I use this code to load the include.js file and then I call testIncludeFn() from it: <code>
15
8211
by: =?Utf-8?B?VG9tIENvcmNvcmFu?= | last post by:
I've been led to believe by several articles, particularly Eric Gunnerson's C# Calling Code Dynamically, that calling a method dynamically through Reflection was much slower than through a Delegate. My testing showed that actually it was six times faster: 0.5 seconds for 100,000 iterations versus 3.1 seconds. Can anyone explain why? Something in the way I coded it? I'd appreciate any insights. Here's the code (in a Windows Form...
6
11581
by: Ole Nielsby | last post by:
VC has a __cdecl specifier which allows functions and methods to be called with varying parameter count. (I understand this is the default for functions in general but in VC, instances use another convention unless they have an ellipsis argument.) I can force GCC and other compilers to use such a convention by declaring all methods with an ellipsis, but I'd rather not clutter my method definitions with these.
0
9591
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10057
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9869
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.