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

ref anonymous method inside the method

It would be handy to be able to ref "this" from inside an AM such as:

(string s)
{
Console.Writeline(s);
DoSomething(this);
}

So treating am like a method of a class (which it is). Currently we have no
context to know so you have to pass that as state which seems kinda
redundant. Naturally, "this" would need to be named something else. Maybe
"that"? ;/ Double-double this-this, double-double that-that, double-this
double-that, double-double this-that.

--
William Stacey [C# MVP]

Jan 6 '07 #1
3 1758
William Stacey [C# MVP] <wi************@gmail.comwrote:
It would be handy to be able to ref "this" from inside an AM such as:

(string s)
{
Console.Writeline(s);
DoSomething(this);
}

So treating am like a method of a class (which it is). Currently we have no
context to know so you have to pass that as state which seems kinda
redundant. Naturally, "this" would need to be named something else. Maybe
"that"? ;/ Double-double this-this, double-double that-that, double-this
double-that, double-double this-that.
I'm not sure I understand what you mean - you already *do* have access
to "this":

using System;

delegate void SimpleDelegate();

class Test
{
string name;

Test(string name)
{
this.name = name;
}

public static void Main()
{
Test t = new Test("Jon");
SimpleDelegate am = t.Foo();
am();
}

SimpleDelegate Foo()
{
string s = "Hello";
return delegate
{
Console.WriteLine (s);
DoSomething(this);
};
}

static void DoSomething(Test t)
{
Console.WriteLine (t.name);
}
}

(I'm sure there's a general purpose parameterless void delegate
somewhere, but I can't remember what it is. I originally used
ThreadStarty, but thought it might confuse things.)
My guess is that you're aware of the above and you're actually after
something else, but it's not clear to me at the moment what that is...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 6 '07 #2
A way for "this" (or some other name) to refer to the anonymous delegate
itself, not the containing class. So for example, inside my anonymous
method I want to call an Unregister() handler to unregister the delegate
from some dispatcher. I need to have a ref to the delegate. And there is
the challenge. I have no way to refer to myself inside the method unless I
also pass that information to the delegate. Like:

// Some anonoumous method.
(string s, delegate self)
{
// use s.
if ( isTrue )
UnRegister(self);
}

This would work, but junks up the delegate signatures.

--
William Stacey [C# MVP]

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
| William Stacey [C# MVP] <wi************@gmail.comwrote:
| It would be handy to be able to ref "this" from inside an AM such as:
| >
| (string s)
| {
| Console.Writeline(s);
| DoSomething(this);
| }
| >
| So treating am like a method of a class (which it is). Currently we have
no
| context to know so you have to pass that as state which seems kinda
| redundant. Naturally, "this" would need to be named something else.
Maybe
| "that"? ;/ Double-double this-this, double-double that-that,
double-this
| double-that, double-double this-that.
|
| I'm not sure I understand what you mean - you already *do* have access
| to "this":
|
| using System;
|
| delegate void SimpleDelegate();
|
| class Test
| {
| string name;
|
| Test(string name)
| {
| this.name = name;
| }
|
| public static void Main()
| {
| Test t = new Test("Jon");
| SimpleDelegate am = t.Foo();
| am();
| }
|
| SimpleDelegate Foo()
| {
| string s = "Hello";
| return delegate
| {
| Console.WriteLine (s);
| DoSomething(this);
| };
| }
|
| static void DoSomething(Test t)
| {
| Console.WriteLine (t.name);
| }
| }
|
| (I'm sure there's a general purpose parameterless void delegate
| somewhere, but I can't remember what it is. I originally used
| ThreadStarty, but thought it might confuse things.)
|
|
| My guess is that you're aware of the above and you're actually after
| something else, but it's not clear to me at the moment what that is...
|
| --
| Jon Skeet - <sk***@pobox.com>
| http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
| If replying to the group, please do not mail me too
Jan 6 '07 #3
William Stacey [C# MVP] <wi************@gmail.comwrote:
A way for "this" (or some other name) to refer to the anonymous delegate
itself, not the containing class.
Ah, right. I'm with you now - although I can't recall ever wanting this
behaviour myself (in .NET 2.0 or other languages that have the concept
of closures).

I'll have a think about any interesting ways of doing it...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 7 '07 #4

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

Similar topics

0
by: Carlos Ribeiro | last post by:
I thought about this problem over the weekend, after long hours of hacking some metaclasses to allow me to express some real case data structures as Python classes. I think that this is something...
0
by: Cordell Lawrence | last post by:
Okay guys, We are wondering if this is a bug in Framework 2.0.40607 and looking for some clarification on the issue. Take a look at the folowing code. public delegate bool BoundryTest(int...
2
by: Marcos Stefanakopolus | last post by:
In C# is there any way to use the concept of delegates to have multiple implementations of a particular block of code, so that you can choose between them without the overhead of possibly expensive...
5
by: cody | last post by:
I have a very funny/strange effect here. if I let the delegate do "return prop.GetGetMethod().Invoke(info.AudioHeader, null);" then I get wrong results, that is, a wrong method is called and I...
12
by: =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?= | last post by:
Hi, I need to initialize my class level dictionary (in .Net 2.0). I wanted to make it inline and employ anonymous methods as I do not use this code for anything else. Something similar to the...
4
by: Frankie | last post by:
I have just gotten up to speed on what anonymous methods are (syntax, capabilities, etc), and how they can be used with /called via delegates. What I am wondering is... 1. Are they only/mostly...
2
by: Gabe Moothart | last post by:
Hello, In one of my asp.net applications, I create a series of checkboxes, set their properties, and give them an "onChecked" event handler on the fly using an anonymous method. The code looks...
2
by: Tony | last post by:
Hello! Here I have some text from a book I read. It says: "An interesting point to note concerning anonymous methods is that they are effectively local to the code block that contains them, and...
0
by: Peter Duniho | last post by:
On Mon, 01 Sep 2008 16:14:10 -0700, Blip <blip@krumpli.comwrote: Briefly, an anonymous method is exactly that: a method without a name. When you use the "delegate" keyword to declare an...
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: 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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...
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.