What's hot ? (and I mean really ...) - scroll down for more
1).  Code Templating - advanced usage of delegates & generics: my slides & demos are available for download! CodeProject article is also available.

2).  My series "TDD in the eyes of a simpleminded" is in progress(including code!): preface, part1, part2, Q&A 1, Manual Stub .vs. Mock Stub

3).  TDD Workshop: SeeCompass v0.1 and v0.2 are out.
# Sunday, May 14, 2006

Preface:

Let's say that you have Button control named myBtn and you want to handle its Click event:

myBtn.Click += new EventHandler(myBtn_Click);

static void myBtn_Click(object sender, EventArgs e) {
   // do something
}


Now, raise your hand if you ever attached the event handler to the event manually, meaning you wrote "new EventHandler(myBtn_Click);" or even specified the method signature "void myBtn_Click(object sender, EventArgs e)".
I don't know about you, but the way I work is by simply writing "myBtn.Click +=" and this magic yellow thingi appears:

autoCompleteEvent.gif

TAB-ing again will generate the method stub and I'm good to go.

Doing this yellow magic on delegates parameters:

We all(?) worked with List<T>. This nice class has some nice methods like ForEach, FindAll, Find etc which get delegate(Action<T>\Predicate<T>) as parameter. for example: 

List<int> numbers = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });

// Print the numbers via ForEach method
numbers.ForEach(
delegate(int i) {
   
Console.Write(i);
});

Would it be great if you can write something like:

numbers.ForEach(

And the yellow thingi will popup and by clicking TAB this code will be generated:

numbers.ForEach(delegate(int i) {
   //TODO
});

This means that this IDE feature should check the type of the delegate - the number and type (even generic type!) of parameters and the return type. This will prevent us from looking at MSDN or "Go To Definition" while working with delegates which are sent as parameters.

Time for you to make a difference:

what say you ? can this save you some time ? If so - leave a comment, I'll do my best to implement it or to forward this request to MS folks (as ladybug).

Sunday, May 14, 2006 7:41:35 PM (Jerusalem Standard Time, UTC+02:00)
That would be a feature I will find most welcome.
Yoni
Sunday, May 14, 2006 9:34:49 PM (Jerusalem Standard Time, UTC+02:00)
This trick look awesome.

1) What are all the options that this one will handle? is it only ForEach and FindAll?
2) will it give a combo for choosing between different delegates?
3) Isn't the ReSharper giving such abilities?

Anyway, if you need help in implementing one of the features... it will be my pleasure to help you.
Sunday, May 14, 2006 11:30:20 PM (Jerusalem Standard Time, UTC+02:00)
Yoni - 10x for your "vote" :)

Shani -
1). for any kind of delegate (even one that you wrote). the IDE will have to be smart enough to probe the delegate(s) types.
2). I thought about this one - a combo should be opened if the method has overloads. this can be totally cool !
3). To be honest, I haven't checked. Is there a kind reader that can check this one ?

If I'll implement it you'll be the first to know. I'm talking with one of the guys from MS which are responsible for those kind of things so I'll keep you all update.
Thursday, May 18, 2006 5:55:08 PM (Jerusalem Standard Time, UTC+02:00)
ReSharper 2.0 does indeed have "Create anonymous delegate for..." and "Create a new method for..." options in its autocomplete box.

I use it all the time to set up UI event handlers that just call some other method to do the real work:

buttonSomeButton.Click += [autocomplete here] ->
buttonSomeButton.Click += delegate(object sender, EventArgs e){
[Put your code here]
};

And then if you don't use the parameters, ReSharper highlights them in grey, and gives you an option to remove them.

Thursday, May 18, 2006 8:49:57 PM (Jerusalem Standard Time, UTC+02:00)
Damn, Resharper 2.0 is simply great.

Ok, I'm sold. I'll buy a license ! :)
Comments are closed.