472,780 Members | 1,186 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

Making sure 2.0 doesn't have blocks

I know 2.0 supports "anonymous methods" (or "closures"), but I don't see
the ability to assign a block of code to a variable (I think they are
simply "blocks" in Ruby).

Am I correct? They seem logically to go hand in hand...
Nov 23 '05 #1
9 1001
Brad,

In order to do this, you would have to assign it to a delegate. You
would do so like this:

// Create a delegate pointing to the block of code.
EventHandler handler =
delegate(object sender, EventArgs e)
{
// Your code here.
};

I believe that if you don't have to access the parameters, you don't
need the delegate keyword, and can just assign the block, but I would double
check.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Brad Wood" <bradley|.wood|@ndsu|.edu> wrote in message
news:uI**************@TK2MSFTNGP09.phx.gbl...
I know 2.0 supports "anonymous methods" (or "closures"), but I don't see
the ability to assign a block of code to a variable (I think they are
simply "blocks" in Ruby).

Am I correct? They seem logically to go hand in hand...

Nov 23 '05 #2
Ah, but C# has always had delegates. If I understand you correctly, a
Block is just an anonymous method, assigned to a delegate.

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
"Brad Wood" <bradley|.wood|@ndsu|.edu> wrote in message
news:uI**************@TK2MSFTNGP09.phx.gbl...
I know 2.0 supports "anonymous methods" (or "closures"), but I don't see
the ability to assign a block of code to a variable (I think they are
simply "blocks" in Ruby).

Am I correct? They seem logically to go hand in hand...

Nov 23 '05 #3
Yes, a regular 1.1 delegate does seem work as a block (has access to
variables within current scope) in 2.0, so this:

string dude;
EventHandler handler = delegate( object sender, EventArgs e )
{ Console.WriteLine( dude ); };
dude = "one";
handler( null, null );
dude = "two";
handler( null, null );

does report:
one
two

Now I can't figure out how why this won't compile...

delegate myBlock = delegate { Console.WriteLine( dude ); };
Nicholas Paldino [.NET/C# MVP] wrote:
Brad,

In order to do this, you would have to assign it to a delegate. You
would do so like this:

// Create a delegate pointing to the block of code.
EventHandler handler =
delegate(object sender, EventArgs e)
{
// Your code here.
};

I believe that if you don't have to access the parameters, you don't
need the delegate keyword, and can just assign the block, but I would double
check.

Nov 23 '05 #4
> Now I can't figure out how why this won't compile...

delegate myBlock = delegate { Console.WriteLine( dude ); };
Because this one is not typed.
Which kind of delegate is it?

Nicholas Paldino [.NET/C# MVP] wrote:
Brad,

In order to do this, you would have to assign it to a delegate. You
would do so like this:

// Create a delegate pointing to the block of code.
EventHandler handler =
delegate(object sender, EventArgs e)
{
// Your code here.
};

I believe that if you don't have to access the parameters, you don't
need the delegate keyword, and can just assign the block, but I would
double check.

Nov 23 '05 #5
That's just it; I don't want a "kind" of delegate, I just want a block of
code, so I suppose I'm just trying to stretch the language beyond where I
should.

A sort of workaround would be a simple delegate type that represents a
method with no parameters. Does such an animal exist? I don't know how to
search the documentation for a list of all delegate types...

"Lloyd Dupont" wrote:
Now I can't figure out how why this won't compile...

delegate myBlock = delegate { Console.WriteLine( dude ); };

Because this one is not typed.
Which kind of delegate is it?


Nov 23 '05 #6
After reading up a bit, as I understand it, in the Lisp/Ruby tradition, a
block is simply a chunk of code kind of like a C macro that can be assigned
to a variable. A closure goes a step further and adds binding to variables
within it's scope even if the object that declared the closure and variables
is no longer in scope.

It appears to me that 2.0 delegates can still act like 1x delegates but can
also be used almost as pure closures; the difference being that you must have
a previously declared delegate type to reference (more strongly typed).

"James Curran" wrote:
Ah, but C# has always had delegates. If I understand you correctly, a
Block is just an anonymous method, assigned to a delegate.


Nov 23 '05 #7
Brad <Br**@discussions.microsoft.com> wrote:
That's just it; I don't want a "kind" of delegate, I just want a block of
code, so I suppose I'm just trying to stretch the language beyond where I
should.

A sort of workaround would be a simple delegate type that represents a
method with no parameters. Does such an animal exist? I don't know how to
search the documentation for a list of all delegate types...


ThreadStart would be an obvious example.

--
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
Nov 23 '05 #8
Jon Skeet [C# MVP] wrote:
Brad <Br**@discussions.microsoft.com> wrote:
That's just it; I don't want a "kind" of delegate, I just want a block of
code, so I suppose I'm just trying to stretch the language beyond where I
should.

A sort of workaround would be a simple delegate type that represents a
method with no parameters. Does such an animal exist? I don't know how to
search the documentation for a list of all delegate types...

ThreadStart would be an obvious example.


I defined two delegates in my class library, AnonymousMethod (takes no
parameters, returns nothing), and AnonymousParameterizedMethod (takes
object as parameter, returns object), just so the meaning is more clear.
No actual difference between ThreadStart and AnonymousMethod but my code
looks cleaner and I avoid the questions I got when I used ThreadStart:
"Hey, where's the thread that is going to execute this threading code?".

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 24 '05 #9
You're exactly right; that's the way to go.

"Lasse Vågsæther Karlsen" wrote:
Jon Skeet [C# MVP] wrote:
Brad <Br**@discussions.microsoft.com> wrote:
That's just it; I don't want a "kind" of delegate, I just want a block of
code, so I suppose I'm just trying to stretch the language beyond where I
should.

A sort of workaround would be a simple delegate type that represents a
method with no parameters. Does such an animal exist? I don't know how to
search the documentation for a list of all delegate types...

ThreadStart would be an obvious example.


I defined two delegates in my class library, AnonymousMethod (takes no
parameters, returns nothing), and AnonymousParameterizedMethod (takes
object as parameter, returns object), just so the meaning is more clear.
No actual difference between ThreadStart and AnonymousMethod but my code
looks cleaner and I avoid the questions I got when I used ThreadStart:
"Hey, where's the thread that is going to execute this threading code?".

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2

Nov 28 '05 #10

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

Similar topics

6
by: Sandman | last post by:
I want to do this: function square ($a){ print "-- $a --"; } And use it like this: <? square( ?> Hello! <? ) ?> And get this:
31
by: CYBER | last post by:
Hello Is there any other way under python to create blocks ?? instead of def sth(x): return x
0
by: Michael Chermside | last post by:
Tim Williams writes: > I have to put my 2 cents in here. I love Python, but the one thing I > miss is using {} or something to enclose blocks. I edit in emacs > python-mode, and believe in...
2
by: Stewart | last post by:
Originally posted in comp.lang.javascript: Newsgroups: comp.lang.javascript From: "Stewart" Date: 23 Aug 2005 02:50:04 -0700 Local: Tues, Aug 23 2005 10:50 am Subject: FireFox, RemoveChild,...
90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
351
by: CBFalconer | last post by:
We often find hidden, and totally unnecessary, assumptions being made in code. The following leans heavily on one particular example, which happens to be in C. However similar things can (and...
14
by: Haines Brown | last post by:
I know I'm missing something obvious. I need a short horizontal rule to preceed a line of text (in a bibliography in which the author is repeated). I tried this: <p> <div class="rule"></div>,...
1
by: yawnmoth | last post by:
I have two tables on the following page: http://www.frostjedi.com/terra/scripts/demo/tables.html I'm trying to make the top one look like the bottom in using only CSS. In FireFox, what I...
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.