473,796 Members | 2,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Indirect method calls ... how to?

Is is possible in C# to have the equivalent of an array of function pointers in C?

I have a situation where a top level class exposes methods like Add, Delete, ... and a few
child classes with the same methods. Depending on a configuration parameter, the child
method will be invoked when the top level class is invoked.

I would like to avoid having to do something like this:

int configuration;

public bool Add()
{
switch (configuration)
{
case 1:
return ChildClass1.Add ();
case 2:
return ChildClass2.Add ();
}
}

I would rather have something like this instead (code not complete)

Arraylist MethodPTR;
int configuration;

public mainClassConstr uctor()
{
// Initialize MethodPTR with the address of the methods

MethodPTR = new ArrayList(8);
configuration = RetrieveConfigu ration();

switch (configuration)
{
case 1:
MethodPTR[0] = ChildClass1.Add ; // Assistance required here
MethodPTR[1] = ChildClass1.Del ete; // Assistance required here
break;
case 2:
MethodPTR[0] = ChildClass2.Add ; // Assistance required here
MethodPTR[1] = ChildClass2.Del ete; // Assistance required here
break;
}
}

public bool Add()
{
return MedthoPTR[0](); // This is where I need assistance
}

public bool Delete()
{
return MedthoPTR[1](); // This is where I need assistance
}

Thank you all in advance.

Gaetan

Jan 30 '06 #1
8 3859
Not really any function pointers.

But....

Take a look at the Reflection classes.

You can get an object that represents a method (MethodInfo class) from a
class and then call the Invoke() method of it to actually execute it. You
could keep an array of MethodInfo objects and treat them sort of like
function pointers.

Also, you can Invoke() a method by using a string representation of the
method name as long as you have an instance of the object that contains it.

Hope this gives you some ideas!

--
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
Charles Cox
VC/VB/C# Developer
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~

"Gaetan" <so*****@somewh ere.com> wrote in message
news:jo******** *************** *********@4ax.c om...
Is is possible in C# to have the equivalent of an array of function
pointers in C?

I have a situation where a top level class exposes methods like Add,
Delete, ... and a few
child classes with the same methods. Depending on a configuration
parameter, the child
method will be invoked when the top level class is invoked.

I would like to avoid having to do something like this:

int configuration;

public bool Add()
{
switch (configuration)
{
case 1:
return ChildClass1.Add ();
case 2:
return ChildClass2.Add ();
}
}

I would rather have something like this instead (code not complete)

Arraylist MethodPTR;
int configuration;

public mainClassConstr uctor()
{
// Initialize MethodPTR with the address of the methods

MethodPTR = new ArrayList(8);
configuration = RetrieveConfigu ration();

switch (configuration)
{
case 1:
MethodPTR[0] = ChildClass1.Add ; // Assistance required here
MethodPTR[1] = ChildClass1.Del ete; // Assistance required here
break;
case 2:
MethodPTR[0] = ChildClass2.Add ; // Assistance required here
MethodPTR[1] = ChildClass2.Del ete; // Assistance required here
break;
}
}

public bool Add()
{
return MedthoPTR[0](); // This is where I need assistance
}

public bool Delete()
{
return MedthoPTR[1](); // This is where I need assistance
}

Thank you all in advance.

Gaetan

Jan 30 '06 #2
Well, first some comments than an answer to your questions.

1) Not really sure if your desired way is any better than the way you are
rejecting. You are still using a switch statement, you are just moving it
to a new location.
2) People who work on this after you may not understand what you are doing.
You would need to document this really well.
3) Although not a big issue, your way would not be as performant because
you are storing pointers to the methods. There would also be the overhead
of calling the methods. (again this is not a big deal, just thought I would
mention it.)

Now, for your answer. The .NET equivalent of function pointers are
Delegates. When you create a delegate, you tell it what method it should
wrap. You can then store the delegate for later use.

On a side note, let me offer some design comments (knowing full well that I
do not have knowledge of what you are trying to accomplish).

When you say "child class", are you referring to inherited subclasses? If
that is the case, then you might want to make Add abstract and then the
correct derived class method will be called, or you can have the base class
have an Add method and the derived classes have an AddImpl method incase you
need the base class to do some additional work. I might be able to offer a
better design if you wanted to share the relationship between the various
classes (i.e. inheritance, composition, etc.).

"Gaetan" <so*****@somewh ere.com> wrote in message
news:jo******** *************** *********@4ax.c om...
Is is possible in C# to have the equivalent of an array of function
pointers in C?

I have a situation where a top level class exposes methods like Add,
Delete, ... and a few
child classes with the same methods. Depending on a configuration
parameter, the child
method will be invoked when the top level class is invoked.

I would like to avoid having to do something like this:

int configuration;

public bool Add()
{
switch (configuration)
{
case 1:
return ChildClass1.Add ();
case 2:
return ChildClass2.Add ();
}
}

I would rather have something like this instead (code not complete)

Arraylist MethodPTR;
int configuration;

public mainClassConstr uctor()
{
// Initialize MethodPTR with the address of the methods

MethodPTR = new ArrayList(8);
configuration = RetrieveConfigu ration();

switch (configuration)
{
case 1:
MethodPTR[0] = ChildClass1.Add ; // Assistance required here
MethodPTR[1] = ChildClass1.Del ete; // Assistance required here
break;
case 2:
MethodPTR[0] = ChildClass2.Add ; // Assistance required here
MethodPTR[1] = ChildClass2.Del ete; // Assistance required here
break;
}
}

public bool Add()
{
return MedthoPTR[0](); // This is where I need assistance
}

public bool Delete()
{
return MedthoPTR[1](); // This is where I need assistance
}

Thank you all in advance.

Gaetan

Jan 30 '06 #3
Check out .Net delegates. This is the equivalent of managed function
pointers.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Gaetan" <so*****@somewh ere.com> wrote in message
news:jo******** *************** *********@4ax.c om...
Is is possible in C# to have the equivalent of an array of function
pointers in C?

I have a situation where a top level class exposes methods like Add,
Delete, ... and a few
child classes with the same methods. Depending on a configuration
parameter, the child
method will be invoked when the top level class is invoked.

I would like to avoid having to do something like this:

int configuration;

public bool Add()
{
switch (configuration)
{
case 1:
return ChildClass1.Add ();
case 2:
return ChildClass2.Add ();
}
}

I would rather have something like this instead (code not complete)

Arraylist MethodPTR;
int configuration;

public mainClassConstr uctor()
{
// Initialize MethodPTR with the address of the methods

MethodPTR = new ArrayList(8);
configuration = RetrieveConfigu ration();

switch (configuration)
{
case 1:
MethodPTR[0] = ChildClass1.Add ; // Assistance required here
MethodPTR[1] = ChildClass1.Del ete; // Assistance required here
break;
case 2:
MethodPTR[0] = ChildClass2.Add ; // Assistance required here
MethodPTR[1] = ChildClass2.Del ete; // Assistance required here
break;
}
}

public bool Add()
{
return MedthoPTR[0](); // This is where I need assistance
}

public bool Delete()
{
return MedthoPTR[1](); // This is where I need assistance
}

Thank you all in advance.

Gaetan

Jan 30 '06 #4
Delegates are "function pointers" in .NET.

"C.C. (aka Me)" <me@home.com> wrote in message
news:jZ******** ************@co mcast.com...
Not really any function pointers.

But....

Take a look at the Reflection classes.

You can get an object that represents a method (MethodInfo class) from a
class and then call the Invoke() method of it to actually execute it. You
could keep an array of MethodInfo objects and treat them sort of like
function pointers.

Also, you can Invoke() a method by using a string representation of the
method name as long as you have an instance of the object that contains
it.

Hope this gives you some ideas!

--
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
Charles Cox
VC/VB/C# Developer
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~

"Gaetan" <so*****@somewh ere.com> wrote in message
news:jo******** *************** *********@4ax.c om...
Is is possible in C# to have the equivalent of an array of function
pointers in C?

I have a situation where a top level class exposes methods like Add,
Delete, ... and a few
child classes with the same methods. Depending on a configuration
parameter, the child
method will be invoked when the top level class is invoked.

I would like to avoid having to do something like this:

int configuration;

public bool Add()
{
switch (configuration)
{
case 1:
return ChildClass1.Add ();
case 2:
return ChildClass2.Add ();
}
}

I would rather have something like this instead (code not complete)

Arraylist MethodPTR;
int configuration;

public mainClassConstr uctor()
{
// Initialize MethodPTR with the address of the methods

MethodPTR = new ArrayList(8);
configuration = RetrieveConfigu ration();

switch (configuration)
{
case 1:
MethodPTR[0] = ChildClass1.Add ; // Assistance required here
MethodPTR[1] = ChildClass1.Del ete; // Assistance required here
break;
case 2:
MethodPTR[0] = ChildClass2.Add ; // Assistance required here
MethodPTR[1] = ChildClass2.Del ete; // Assistance required here
break;
}
}

public bool Add()
{
return MedthoPTR[0](); // This is where I need assistance
}

public bool Delete()
{
return MedthoPTR[1](); // This is where I need assistance
}

Thank you all in advance.

Gaetan


Jan 30 '06 #5
> Is is possible in C# to have the equivalent of an array of function
pointers in C?

I have a situation where a top level class exposes methods like Add,
Delete, ... and a few child classes with the same methods. Depending
on a configuration parameter, the child method will be invoked when
the top level class is invoked.


I would use delegates for this. The following example is not very elegant
but, it shows the basic use of delegates and from what I read, it should
apply to your situation.

Chris

---

class Class1
{

[STAThread]
static void Main(string[] args)
{
Class1 class1 = new Class1(0);
Class1 class2 = new Class1(1);

class1.Add();
class1.Delete() ;

class2.Add();
class2.Delete() ;

Console.ReadLin e();
}

public delegate void TakeCarOfStuffH andler();

TakeCarOfStuffH andler addHandler;
TakeCarOfStuffH andler deleteHandler;

public Class1(int i)
{
ChildClass1 class1 = new ChildClass1();
ChildClass2 class2 = new ChildClass2();

switch(i)
{
case 0:
addHandler = new TakeCarOfStuffH andler(class1.A dd);
deleteHandler = new TakeCarOfStuffH andler(class1.D elete);
break;
case 1:
addHandler = new TakeCarOfStuffH andler(class2.A dd);
deleteHandler = new TakeCarOfStuffH andler(class2.D elete);
break;
}
}

public void Add()
{
addHandler();
}

public void Delete()
{
deleteHandler() ;
}
}

class ChildClass1
{
public void Add()
{
Console.WriteLi ne("ChildClass1 .Add()");
}

public void Delete()
{
Console.WriteLi ne("ChildClass1 .Delete()");
}
}

class ChildClass2
{
public void Add()
{
Console.WriteLi ne("ChildClass2 .Add()");
}

public void Delete()
{
Console.WriteLi ne("ChildClass2 .Delete()");
}
}
Jan 30 '06 #6
I don't understand how your problem is different from standard
polymorphism:

public class Base
{
public virtual bool Add() { ... }
public virtual bool Delete() { ... }
}

public class Child : Base
{
public override bool Add() { ... }
public override bool Delete() { ... }
}

then

Base aBase = new Child();
aBase.Add();

I'm sure I'm missing something here... how do your requirements differ
from what's offered by polymorphism?

Jan 30 '06 #7

Thanks Peter for your explanations. I have taken a closer look at using either Delegates
or an abstract class. I think that what I'm trying to do could be more easily implemented
with a base class and derived classes.
On Mon, 30 Jan 2006 10:21:42 -0800, "Peter Rilling" <pe***@nospam.r illing.net> wrote:
Well, first some comments than an answer to your questions.

1) Not really sure if your desired way is any better than the way you are
rejecting. You are still using a switch statement, you are just moving it
to a new location.
2) People who work on this after you may not understand what you are doing.
You would need to document this really well.
3) Although not a big issue, your way would not be as performant because
you are storing pointers to the methods. There would also be the overhead
of calling the methods. (again this is not a big deal, just thought I would
mention it.)

Now, for your answer. The .NET equivalent of function pointers are
Delegates. When you create a delegate, you tell it what method it should
wrap. You can then store the delegate for later use.

On a side note, let me offer some design comments (knowing full well that I
do not have knowledge of what you are trying to accomplish).

When you say "child class", are you referring to inherited subclasses? If
that is the case, then you might want to make Add abstract and then the
correct derived class method will be called, or you can have the base class
have an Add method and the derived classes have an AddImpl method incase you
need the base class to do some additional work. I might be able to offer a
better design if you wanted to share the relationship between the various
classes (i.e. inheritance, composition, etc.).

Jan 30 '06 #8
Thanks Bruce ... this is the way I will go.

On 30 Jan 2006 10:37:17 -0800, "Bruce Wood" <br*******@cana da.com> wrote:
I don't understand how your problem is different from standard
polymorphism :

public class Base
{
public virtual bool Add() { ... }
public virtual bool Delete() { ... }
}

public class Child : Base
{
public override bool Add() { ... }
public override bool Delete() { ... }
}

then

Base aBase = new Child();
aBase.Add();

I'm sure I'm missing something here... how do your requirements differ
from what's offered by polymorphism?

Jan 30 '06 #9

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

Similar topics

2
1975
by: Matt Leslie | last post by:
I want to write a package to simulate a distributed system with objects at various locations interacting with each other. What I'd like to do is simulate network delays between distant objects using an event queue. That is, when I call A.B(foo), I want this call to be intercepted somehow, and the call to B added to a central event queue thread, and is called later, possibly after other previously enqued method calls. The central queue...
1
1482
by: Yasutaka Ito | last post by:
Hi, Given an object, I want to programmatically monitor it for any property changes and method calls. The object can be anything (component, control, etc.), and there is no guarantee that each property has <property name>Changed event corresponding to it. What are the best way for this? I'm sorry, I don't know where to get started from...
0
1621
by: Sham Ramakrishnan | last post by:
guys... I have a SOAP service at my clients that I have to use to call the exposed methods... well... there are supposedly 3 ways in which I can look at doing it... 1. Create a proxy for the SOAP service using the WSDL.exe utility and use the proxy directly by creating instances of the proxy class like a SOAP Client. 2. Create a proxy for the SOAP service using the WSDL.exe utility, compile it into an assembly and use the resulting...
4
1803
by: Lawrence Oluyede | last post by:
I've never used metaclasses in real life before and while searching through the online Cookbook I found this gorgeous example: "Wrapping method calls (meta-class example)" http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/198078 What I want to do in my app is to log all method calls and seems that the metaclass example above do this fine but it fails when it's needed to instantiate the class I'm in in a method. So if I do:
4
2373
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of which I mean here. Just for a moment, let's just take one definition for one of the
0
920
by: Cody | last post by:
I will be using a FW that contains no documentation. In order to learn the FW, I would like to have a utility record all the method calls made while I run an application that uses the FW. I would then like to see a tree view of the calls that where made. Example: -main() | - GetData() | - ShowData()
0
1071
by: JohnV | last post by:
Group, I'm writing a dotnet windows service that will create and maintain a collection of COM components. These components all implement the same interface, and some are written in VC++ 6 and some written in C# (registered for COM interop). Once every minute, the windows service will need to call an "Execute" method on each component in the collection, and here's the thing: I need those method calls to happen asynchronously. So they...
1
1219
by: Gurur | last post by:
how can we keep track of function and method calls without writing an inch of code in the method to trap the method name from outside the method. Do we have any function to keep track of all the methods name... Is it possible of keeping track of method names of an application from a different application..
0
1042
bartonc
by: bartonc | last post by:
#----------------------------------------------------------------------------- # Name: ObjectRedirect.py # Purpose: Demonstrate class methode redirection with new-style classes # # Author: Barton # # Created: 2007/09/20 # RCS-ID: $Id: ObjectRedirect.py $ # Copyright: (c) 2007 # Licence: free, free, free
0
9685
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
10459
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
10237
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...
0
10018
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
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6795
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();...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
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
3
2928
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.