473,320 Members | 1,713 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,320 software developers and data experts.

Delegate constructor types

The first type to a delegate constructor is obvious. It is a System::Object
* . What is the MC++ type of the second argument to the delegate constructor
? In C++ it would be a member function pointer when the first object is not
0, but I can't find its equivalent type in .NET. I would like to save an
object of that type and use that object to construct a delegate ? As an
example:

public __delegate void ADelegate();

__gc public class X
{
public:
void AMethod();
};

// Normally
X * myX = new X;
ADelegate * myADel = new ADelegate(myX,X::AMethod);
myADel(); // Calls myX::AMethod();

But let's suppose I want to save the actual method to be called, which is
the second argument to a delegate constructor. Is there a .NET type for this
? If there is, can I create an object of that type passing it a member
function ?

As an example, using the above delegate and class, let's suppose the .NET
type is called System::MemberReference and it takes a member function as a
parameter to its constructor. I could then do:

System::MemberReference * mObjMethod = new System::Method(X::AMethod);
// And then later
X * myX = new X;
ADelegate * myADel = new ADelegate(myX,mObjMethod);
myADel(); // Calls myX::AMethod();

Is this possible in some way ?

In standard C++ the concept of a member pointer exists as a pointer to a
member function of a particular class. It has the form of:

returnValue SomeClass::*SomeMethod(someParameters);

and one can say:

SomeMethod = SomeClass::AMethod;

as long as AMethod was a member function of SomeClass and had the same
signature as SomeMethod.

But I don't think I can use standard C++ when my object is a pointer to a
__gc class and my method is a member function of that __gc class.

Why would I want to do this in .NET, you say ? Because sometimes I don't
know until later what object of a certain type I want to use but I know the
member function(s) of a class of a particular signature I want to call. So
if I could create an instance of just a member function, equivalent to the
C++ member function pointer, I could pass that to another function which
will find the object, create the delegate and invoke it. I can't create the
delegate and pass that because I don't immediately know the object, but I do
know the member function(s). Finally the member function signature might be
one of a number of identical member functions of the same type for a
particular class, but the eventual code which invokes the delegate wouldn't
know or care about this. For what I want to do, C++'s ability to bind a
member function pointer to an object works better than .NET delegates which
automatically does the binding . Unfortunately I can not find out a .NET
equivalent to a C++ member function pointer in order to do this binding into
a delegate.
Nov 17 '05 #1
5 3592
You can do this as follows:

public __delegate void ADelegate();

__gc __interface IDelegateFactory {
ADelegate CreateDelegate(X *x);
};

__gc class DelegateFactoryForAMethod : public IDelegateFactory {
public:
ADelegate CreateDelegate(X *x) {
return new ADelegate(x, X::AMethod);
}
};

__gc class DelegateFactoryForBMethod : public IDelegateFactory {
public:
ADelegate CreateDelegate(X *x) {
return new ADelegate(x, X::BMethod);
}
};

IDelegateFactory *factory = new DelegateFactoryForAMethod();
// and then later
X * myX = new X;
ADelegate * myADel = factory->CreateDelegate(myX);
myADel(); // Calls myX::AMethod();

Greetings

Bart Jacobs

Nov 17 '05 #2
Bart Jacobs wrote:
You can do this as follows:

public __delegate void ADelegate();

__gc __interface IDelegateFactory {
ADelegate CreateDelegate(X *x);
};

__gc class DelegateFactoryForAMethod : public IDelegateFactory {
public:
ADelegate CreateDelegate(X *x) {
return new ADelegate(x, X::AMethod);
}
};

__gc class DelegateFactoryForBMethod : public IDelegateFactory {
public:
ADelegate CreateDelegate(X *x) {
return new ADelegate(x, X::BMethod);
}
};

IDelegateFactory *factory = new DelegateFactoryForAMethod();
// and then later
X * myX = new X;
ADelegate * myADel = factory->CreateDelegate(myX);
myADel(); // Calls myX::AMethod();


A good idea. Thanks !
Nov 17 '05 #3
Edward,
The first type to a delegate constructor is obvious. It is a System::Object * . What is the MC++ type of the second argument to the delegate constructor ? In C++ it would be a member function pointer when the first object is not 0, but I can't find its equivalent type in .NET. I would like to save an
object of that type and use that object to construct a delegate ?


An easier way would just be to use the CreateDelegate() static method of the
Delegate class. You can create the delegate using, for example, the object
instance and the name of the method.

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #4
Tomas Restrepo (MVP) wrote:
Edward,
The first type to a delegate constructor is obvious. It is a
System::Object * . What is the MC++ type of the second argument to
the delegate constructor ? In C++ it would be a member function
pointer when the first object is not 0, but I can't find its
equivalent type in .NET. I would like to save an object of that type
and use that object to construct a delegate ?


An easier way would just be to use the CreateDelegate() static method
of the Delegate class. You can create the delegate using, for
example, the object instance and the name of the method.


I see the CreateInstance methods. Very cool !

Is there any overhead to using reflection ? Something tells me it is a bit
slower than the way which Mr. Jacobs suggested, which I do tend to prefer. I
am just a little old-fashioned in this respect, not having used reflection
much. C++ doesn't have it and, although Python and Java do have it, I
haven't used it there before.
Nov 17 '05 #5
Hi Edward,
I see the CreateInstance methods. Very cool !

Is there any overhead to using reflection ? Something tells me it is a bit
slower than the way which Mr. Jacobs suggested, which I do tend to prefer. I am just a little old-fashioned in this respect, not having used reflection
much. C++ doesn't have it and, although Python and Java do have it, I
haven't used it there before.


There is certainly some overhead to using reflection, but in many cases it
might not be very significant. I'd just do some profiling to try out,
because it's a really powerful feature (and in your case, it would indeed
save you a LOT of work if you're dealing with many delegate types!)

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #6

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

Similar topics

2
by: Edward Diener | last post by:
The first type to a delegate constructor is obvious. It is an object * . What is the .NET type of the second argument to the delegate constructor ? Can I save an object of that type and use that...
4
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
15
by: Sharon | last post by:
I’m trying to build a generic Publisher-Subscriber that will work over the net, so I’m using the Remoting. I wish that the subscriber user will be notify about the messages sent by the...
6
by: Marty | last post by:
Hi, I would like to have my class to execute eventually an outside function. I was thinking to pass to my class constructor a delegate object that would be later used in my class to be...
3
by: IMS.Rushikesh | last post by:
Hi All, There is a scenario, in which there is a counter in my base class. There is some initial value of this counter (check below code). Now there is a function "DecreaseCount", which will...
6
by: RobKinney1 | last post by:
Hello, This may be an easy question, but I cannot stumle upon the correct way of doing this. I have a function in a base class. I then pass it to a class as a delegate so I don't have to...
10
by: Nathan Laff | last post by:
I have a custom attribute which i use for fields in an enum. I want to pass around a delegate in these things. so i want to do something like this Is this possible? i'm having no luck.
6
by: Sergey Poberezovskiy | last post by:
I have the following code in C# that I have trouble converting to VB(2.0): private delegate void openDialog(); private void openWindowsDialog(openDialog open) { Thread thread = new Thread(new...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
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...
1
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.