473,396 Members | 2,033 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,396 software developers and data experts.

Delegate that works as a Pointer to a member function?

Hi,

I need the equivalent of a pointer to a member function (as used in C++) but
in C# 2.0. I know it must be a special way of using delegates but don't know
how to make the function associated with the delegate capable of accessing
the private parts of the class.

For example, in C++ one would have:

typedef bool (CSomeClass::*TAGTEXTFUNC)( CString &rstr );

so that we could assign any method of CSomeClass to TAGTEXTFUNC, and be able
to access private parts of CSomeClass. How can one accomplish this in C#?
--
Thanks in advance,

Juan Dent, M.Sc.
Sep 13 '07 #1
4 3927
Juan,

What you have isn't able to be assigned any function from CSomeClass, it
has to have the specific signature that takes a CString and returns a bool.

In .NET you want to use a delegate. For example, to take a string and
return a boolean, you have the generic Predicate<Tdelegate, which is
defined as:

// Item might not be the actual parameter name.
delegate bool Predicate<T>(T item);

If you had a class like this:

public class MyClass
{
public bool DoSomething(string item)
{
// Return some boolean value based on item.
return item.Length 10;
}
}

You could then code this:

MyClass myClass = new MyClass();
Predicate<stringdel = myClass.DoSomething;

You could then pass around del and call it like a method anywhere you
wish.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Juan Dent" <ju******@nospam.nospamwrote in message
news:FD**********************************@microsof t.com...
Hi,

I need the equivalent of a pointer to a member function (as used in C++)
but
in C# 2.0. I know it must be a special way of using delegates but don't
know
how to make the function associated with the delegate capable of accessing
the private parts of the class.

For example, in C++ one would have:

typedef bool (CSomeClass::*TAGTEXTFUNC)( CString &rstr );

so that we could assign any method of CSomeClass to TAGTEXTFUNC, and be
able
to access private parts of CSomeClass. How can one accomplish this in C#?
--
Thanks in advance,

Juan Dent, M.Sc.
Sep 13 '07 #2
Juan,

To get around the protection level check:

static void Main(string[] args)
{
MyClass p = new MyClass();
Action func =
(Action)Delegate.CreateDelegate(typeof(Action), p,
"PrivateMethod");
func.Invoke();
}

~James

Sep 13 '07 #3
Of course, one should consider that if the method was made private, then
perhaps it shouldn't be called by outside code, or, if the coder controls
the class, then their design is wrong?

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"james" <ja********@gmail.comwrote in message
news:11*********************@y42g2000hsy.googlegro ups.com...
Juan,

To get around the protection level check:

static void Main(string[] args)
{
MyClass p = new MyClass();
Action func =
(Action)Delegate.CreateDelegate(typeof(Action), p,
"PrivateMethod");
func.Invoke();
}

~James
Sep 13 '07 #4
Nicholas Paldino [.NET/C# MVP] wrote:
Of course, one should consider that if the method was made private,
then perhaps it shouldn't be called by outside code, or, if the coder
controls the class, then their design is wrong?
Not only do I agree with you, I think you could have put it a little
more strongly. :)

Access modifiers are there for a reason. If the code creating the
delegate can't call the method directly, it has no business using the
method to create a delegate. Not everything one _can_ do is an example
of something one _should_ do.

Pete
Sep 13 '07 #5

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

Similar topics

5
by: Edward Diener | last post by:
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...
2
by: Russell Hind | last post by:
I have a delegate which I use to store a current 'state' function (for a statemachine inside a form). __delegate void State_t(const Message_c& Message); I assign to it such as m_State = new...
11
by: Florian A. | last post by:
Hi everyone! I'm trying to write a global hook in C# and C++/CLI. I'm almost done if there wasn't this little delegate problem. I get the function pointer to a delegate and pass it to my...
4
by: ^MisterJingo^ | last post by:
Hi all, I've been trying to get my head around delegates. The book i'm using had a single example, not much explaination, and didn't show how to set up a delegate and pass variables in and out...
2
by: zamir.khan | last post by:
Hello all, New to the groups, sorry if this the wrong forum/etiquette. I am coding a c++ application that requires the use of a timer-triggered event handler. I decided to use the timer provided...
8
by: Jeff S. | last post by:
I was recently advised: << Use List<struct> and Find() using different Predicate delegates for your different search needs.>> What is "Predicate delegate" as used in the above recommendation? ...
0
by: Haxan | last post by:
Hi, I have an unmanaged application that converts a function pointer to a delegate and then pass this as a parameter(delegate) to a managed function which then invokes it. Currently Im able to...
3
by: lothar.behrens | last post by:
Hi, I am thinking about the delegate mechanism and try to understand it. I am coming from C++ and know about callbacks or member callbacks. In C++ I have this typedef for every class that...
10
by: vcquestions | last post by:
Hi. Is there way to have a function pointer to a delegate in c++/cli that would allow me to pass delegates with the same signatures as parameters to a method? I'm working with managed code. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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...
0
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...
0
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,...
0
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...

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.