473,808 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to call an object method named in a string variable ???

I want to build a generic application that runs one of a set of class
methods depending on data extracted from a database table.

For example if I extract value X from the database table I also extract a
string variable that contains the corresponding method name.

string sMethodName = null;
Class1 oClass1 = new Class1( );

If Class1 has a method named Method1 and the variable sMethodName =
"Method1"

How do I construct a call to oClass1.Method1 ( ) using the >> contents << of
the MethodName variable ???

Thanks in advance.

Barry
in Oregon


Nov 15 '05 #1
4 1693
Hi BBFrost,

You can use Reflection to do that

MethodInfo mi = oClass1.GetType ().GetMethod(sM ethodName);
if(mi != null) mi.Invoke(oClas s, new object[]{<method parameters go here>});

Depending on the method visibility you may need to call an overload of
GetMethod that takes BindingFlags parameter.

--
HTH
B\rgds
100

"BBFrost" <barry.b.frost@ remove_this.wrd .state.or.us> wrote in message
news:Ol******** *******@tk2msft ngp13.phx.gbl.. .
I want to build a generic application that runs one of a set of class
methods depending on data extracted from a database table.

For example if I extract value X from the database table I also extract a
string variable that contains the corresponding method name.

string sMethodName = null;
Class1 oClass1 = new Class1( );

If Class1 has a method named Method1 and the variable sMethodName =
"Method1"

How do I construct a call to oClass1.Method1 ( ) using the >> contents << of the MethodName variable ???

Thanks in advance.

Barry
in Oregon

Nov 15 '05 #2
Hi BBFrost

You can look at Using Activator Class. as well as Type class (see msdn doc)

This will allow you to dynamically create, manage classes and methods.

Henk

"BBFrost" <barry.b.frost@ remove_this.wrd .state.or.us> wrote in message
news:Ol******** *******@tk2msft ngp13.phx.gbl.. .
I want to build a generic application that runs one of a set of class
methods depending on data extracted from a database table.

For example if I extract value X from the database table I also extract a
string variable that contains the corresponding method name.

string sMethodName = null;
Class1 oClass1 = new Class1( );

If Class1 has a method named Method1 and the variable sMethodName =
"Method1"

How do I construct a call to oClass1.Method1 ( ) using the >> contents << of the MethodName variable ???

Thanks in advance.

Barry
in Oregon

Nov 15 '05 #3
Great!

Sounds like that will work. The methods I'll be calling with be exposed as
'public' so there shouldn't be any problem. Thanks for the quick reply!
Its very much appreciated.

I'll give it a try and post back with the results.

Thanks again.

Barry
in Oregon

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:uh******** ******@TK2MSFTN GP12.phx.gbl...
Hi BBFrost,

You can use Reflection to do that

MethodInfo mi = oClass1.GetType ().GetMethod(sM ethodName);
if(mi != null) mi.Invoke(oClas s, new object[]{<method parameters go here>});
Depending on the method visibility you may need to call an overload of
GetMethod that takes BindingFlags parameter.

--
HTH
B\rgds
100

"BBFrost" <barry.b.frost@ remove_this.wrd .state.or.us> wrote in message
news:Ol******** *******@tk2msft ngp13.phx.gbl.. .
I want to build a generic application that runs one of a set of class
methods depending on data extracted from a database table.

For example if I extract value X from the database table I also extract a string variable that contains the corresponding method name.

string sMethodName = null;
Class1 oClass1 = new Class1( );

If Class1 has a method named Method1 and the variable sMethodName =
"Method1"

How do I construct a call to oClass1.Method1 ( ) using the >> contents <<

of
the MethodName variable ???

Thanks in advance.

Barry
in Oregon


Nov 15 '05 #4
I have it working. Many Thanks!

Barry
in Oregon

"BBFrost" <barry.b.frost@ remove_this.wrd .state.or.us> wrote in message
news:#5******** ******@tk2msftn gp13.phx.gbl...
Great!

Sounds like that will work. The methods I'll be calling with be exposed as 'public' so there shouldn't be any problem. Thanks for the quick reply!
Its very much appreciated.

I'll give it a try and post back with the results.

Thanks again.

Barry
in Oregon

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:uh******** ******@TK2MSFTN GP12.phx.gbl...
Hi BBFrost,

You can use Reflection to do that

MethodInfo mi = oClass1.GetType ().GetMethod(sM ethodName);
if(mi != null) mi.Invoke(oClas s, new object[]{<method parameters go here>});

Depending on the method visibility you may need to call an overload of
GetMethod that takes BindingFlags parameter.

--
HTH
B\rgds
100

"BBFrost" <barry.b.frost@ remove_this.wrd .state.or.us> wrote in message
news:Ol******** *******@tk2msft ngp13.phx.gbl.. .
I want to build a generic application that runs one of a set of class
methods depending on data extracted from a database table.

For example if I extract value X from the database table I also
extract a string variable that contains the corresponding method name.

string sMethodName = null;
Class1 oClass1 = new Class1( );

If Class1 has a method named Method1 and the variable sMethodName =
"Method1"

How do I construct a call to oClass1.Method1 ( ) using the >> contents
<< of
the MethodName variable ???

Thanks in advance.

Barry
in Oregon



Nov 15 '05 #5

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

Similar topics

5
2694
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one function to verify the name fields, age, email and gender. My question is: if I create a function for each field like the code below, what would be the best way to organize the functions and call them? Would I need one main function and place...
0
1497
by: Mark | last post by:
Hi all, i'm trying to serialize a class. Using the constructor of XmlSerializer i get these (odd?) errors: "File or assembly name goseij9w.dll, or one of its dependencies, was not found". Everytime i run the testprogram it complains about another exotically named dll like et_kn-hl.dll or afeaqisr.dll. Even the example from msdn throws these errors. Anyone knows wuts wrong?
6
2344
by: Luke | last post by:
Here is my emails to Danny Goodman (but probably he is very busy so he didn't answered it). First email(simple): Subject: JavaScript Arrays " We all know the array can act like HashMap, but is there a shortcut for creating such array ? eg //simple array indexed via numbers var a = new Array("a", "fdsf", "sada"); or,
5
1924
by: Matt Clepper | last post by:
Any way to do this? I need to call functions based on a variable. Do I actually have to make a case statement and call each funciton explicitly, or is there any way to call a function where the funciton name is a variable. Example: dim variable as string variable = "thisfunction()" call variable <---this will not work
10
8213
by: Clint | last post by:
Hey all - I'm having a really confusing problem concerning a web service. Right now, I have an application that needs to call a web service that does nothing but return "true" (this will obviously change once the program's fully built to actually do something, but for testing, it works). The only code I added to the service is below:
1
2642
by: keith.walter | last post by:
My first asp.net app is almost "done" and I am stuck. Here is my situation: I have a "mother" page add_customer.aspx and a"child" user control add_group.ascx. On the mother page is an "add group" button that makes the the panel inwhich the add_customer control resides VISIBLE=True. On the control, the user can edit the list of groups, or add a new one. After the edit is complete I want to make the panel again VISIBLE=False, as well as...
7
2863
by: Arpan | last post by:
The .NET Framework 2.0 documentation states that An Object variable always holds a pointer to the data, never the data itself. Now w.r.t. the following ASP.NET code snippet, can someone please explain me what does the above statement mean? <script runat="server"> Class Clock
3
4037
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 selects? thanks...
21
55640
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
0
9721
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
9600
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
10631
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10374
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
10374
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
10114
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...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
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();...
3
3011
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.