473,320 Members | 1,930 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 problem

I can't figure out why this code does not compile - says that 'num' in an
undeclared identifier and that 'delegate':identifier not found. Thoughts?
Help?

void MethodD()
{
List<int>^ data;
BinaryFormatter^ formatter = gcnew BinaryFormatter();
MemoryStream^ stream = gcnew MemoryStream();
data->ForEach(delegate(int num)
{
formatter->Serialize(stream, num);
}
);
};
Nov 17 '05 #1
5 1309
OK, so I now know that anonymous methods (as below) are not supported in
C++/CLI. How then, would one implement the ForEach method of List<> below in
CLI? I have searched the online docs and cant find an example. Thanks again.

\K

"karch" <no****@spamu.com> wrote in message
news:OK**************@TK2MSFTNGP09.phx.gbl...
I can't figure out why this code does not compile - says that 'num' in an
undeclared identifier and that 'delegate':identifier not found. Thoughts?
Help?

void MethodD()
{
List<int>^ data;
BinaryFormatter^ formatter = gcnew BinaryFormatter();
MemoryStream^ stream = gcnew MemoryStream();
data->ForEach(delegate(int num)
{
formatter->Serialize(stream, num);
}
);
};

Nov 17 '05 #2
karch wrote:
OK, so I now know that anonymous methods (as below) are not supported
in C++/CLI. How then, would one implement the ForEach method of
List<> below in CLI? I have searched the online docs and cant find an
example. Thanks again.


Same way you would in C# 1.1 - the method can't be anonymous (i.e. it must
have a name, and a signature that conforms to the appropriate delegate type,
etc).

-cd
Nov 17 '05 #3
Could you give me an example - I've only seen it done with anonymous methods
in C#. And, since generics are new in 2.0, along with the List<>->ForEach
method which takes an Action, I'm not sure how to do this.

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:%2****************@TK2MSFTNGP15.phx.gbl...
karch wrote:
OK, so I now know that anonymous methods (as below) are not supported
in C++/CLI. How then, would one implement the ForEach method of
List<> below in CLI? I have searched the online docs and cant find an
example. Thanks again.


Same way you would in C# 1.1 - the method can't be anonymous (i.e. it must
have a name, and a signature that conforms to the appropriate delegate
type, etc).

-cd

Nov 17 '05 #4
"karch" <no****@spamu.com> wrote in message
news:uB**************@tk2msftngp13.phx.gbl...
Could you give me an example - I've only seen it done with anonymous
methods in C#. And, since generics are new in 2.0, along with the
List<>->ForEach method which takes an Action, I'm not sure how to do this.


Well, since your anonymous delegate references local variables (technically
a "closure"), it's a little more complicated. You'd do something like this:

// Sample

public ref class X
{
ref class Capture
{
private:
BinaryFormatter^ _formatter;
MemoryStream^ _stream;

public:
Capture(BinaryFormatter^ formatter, MemoryStream^ stream)
: _formatter(formatter), _stream(stream)
{
}

void Action(int n)
{
_formatter->Serialize(_stream,n);
}
};

public:
void IterateList()
{
List<int>^ data;
BinaryFormatter^ formatter = gcnew BinaryFormatter();
MemoryStream^ stream = gcnew MemoryStream();
Capture c(formatter,stream);
data->ForEach(gcnew Action<int>(%c,&Capture::Action));
}
};

// End of sample

The class Capture binds the two local variables to member variables so
they're accessible to the Action function. If formatter and stream were
already members of the class containing IterateList (MethodD in your
example), then you wouldn't need that intermediate capture class.

Under the covers, the C# compiler implements something very similar to this
when you write an anonymous method, automatically generating captures for
local variables (and hoisting local variables of value-type to the GC heap
so that the closure can modify them too). Pretty handy mechanism, isn't it?
Maybe some day C++ will have closures, but that day isn't today.

-cd


Nov 17 '05 #5
Thanks Carl. Great explanation - yes, I guess I never really appreciated the
power of anonymous methods to the extent that I should.:-)

(and thanks for the code sample, as well - its always easier to see code in
action)

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:eR**************@TK2MSFTNGP15.phx.gbl...
"karch" <no****@spamu.com> wrote in message
news:uB**************@tk2msftngp13.phx.gbl...
Could you give me an example - I've only seen it done with anonymous
methods in C#. And, since generics are new in 2.0, along with the
List<>->ForEach method which takes an Action, I'm not sure how to do
this.


Well, since your anonymous delegate references local variables
(technically a "closure"), it's a little more complicated. You'd do
something like this:

// Sample

public ref class X
{
ref class Capture
{
private:
BinaryFormatter^ _formatter;
MemoryStream^ _stream;

public:
Capture(BinaryFormatter^ formatter, MemoryStream^ stream)
: _formatter(formatter), _stream(stream)
{
}

void Action(int n)
{
_formatter->Serialize(_stream,n);
}
};

public:
void IterateList()
{
List<int>^ data;
BinaryFormatter^ formatter = gcnew BinaryFormatter();
MemoryStream^ stream = gcnew MemoryStream();
Capture c(formatter,stream);
data->ForEach(gcnew Action<int>(%c,&Capture::Action));
}
};

// End of sample

The class Capture binds the two local variables to member variables so
they're accessible to the Action function. If formatter and stream were
already members of the class containing IterateList (MethodD in your
example), then you wouldn't need that intermediate capture class.

Under the covers, the C# compiler implements something very similar to
this when you write an anonymous method, automatically generating captures
for local variables (and hoisting local variables of value-type to the GC
heap so that the closure can modify them too). Pretty handy mechanism,
isn't it? Maybe some day C++ will have closures, but that day isn't today.

-cd

Nov 17 '05 #6

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

Similar topics

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...
5
by: Doug Handler | last post by:
Hi, I have a form (Form1) that contains a tab control which one tab has a customer user control (UserControl1). When the user double-clicks on the grid hosted there a new user control is...
1
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
4
by: Bill Woodruff | last post by:
< note : this message was sparked in part by comments by David Browne on a previous thread : "inserting an anonymous method as a value in a generic dictionary ?" : David had shown the use of...
11
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
7
by: Andreas Reiff | last post by:
Hey! I have a managed c++ app (actually, the app is mixed managed/unmanaged, but this happens in the managed part) that dynamically generates some program in c# (with CSharpCodeProvider). The...
3
by: james | last post by:
Hi guys, I create a delegate and pass in a local variable. When the variable is a reference type everything works fine, but when it is a valuetype the delegate uses the value of the last...
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...
8
by: Advait Mohan Raut | last post by:
Hello, I am using VC# 2005 ; C# 2.0 I am working on the performance measurement tool. For that I need my code to call user defined method along with its parameters. For that should I use a...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: 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...
0
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...
0
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.