473,785 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Writeli ne(s);
DoSomething(thi s);
}

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 1775
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.Writeli ne(s);
DoSomething(thi s);
}

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.WriteLi ne (s);
DoSomething(thi s);
};
}

static void DoSomething(Tes t t)
{
Console.WriteLi ne (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.co m>
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.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
| 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.Writeli ne(s);
| DoSomething(thi s);
| }
| >
| 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.WriteLi ne (s);
| DoSomething(thi s);
| };
| }
|
| static void DoSomething(Tes t t)
| {
| Console.WriteLi ne (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.co m>
| 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.co m>
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
2069
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 with potential to be useful, but I would like to hear more opinions first. If this is deemed to be useful, I *may* try to write a PEP for it. This is not a promise or even a proposal, at this point. Broadly generalizing, classes in Python have...
0
1654
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 myVal);
2
1581
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 if() or switch() logic, and without the overhead of a function call? As an example, consider the task of resizing a bitmap from size X1,Y1 to size X2,Y2 (may be shrunk or expanded, may or may not maintain aspect ratio). Code that handles the...
5
1696
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 have no clue why. But if I store the MethodInfo in a local variable I works as expected. I do not understand why this is so, shouldn't both ways be semantically equal?
12
2713
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 following: private static Dictionary<string, field_fields = delegate() { Dictionary<string, fieldresult = new Dictionary<string, field>(); result.Add("Application", new field("Application", "AppName"));
4
2394
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 syntactic sugar (important as that can be)? 2. Are anonymous methods *required* anywhere? That is, are anonymous methods the only way to accomplish certain tasks in ..NET programming?
2
2171
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 like this: protected void Page_Load(object sender, EventArgs e) { Dictionary<Guid, stringdataHash = ... //db access foreach(KeyValuePair<Guid, stringrow in dataHash) {
2
1849
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 they have access to local variables in this scope. If you use such a variable it becomes an outer variable. Outer variables are not disposed of when they go out of scope like other local variables are; instead, they live on until the anonymous...
0
1422
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 anonymous method, all you're doing is writing a method the same as you would anywhere else, except that it doesn't have a name, and so you have to use it right away rather than being able to refer to it elsewhere. (And I mean that only in the static,...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
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
10147
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...
1
10087
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
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
6737
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
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
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.