There’s no with keyword in C#, like Visual Basic. So you end up writing code like this:
this.StatusProgressBar.IsIndeterminate = false; this.StatusProgressBar.Visibility = Visibility.Visible; this.StatusProgressBar.Minimum = 0; this.StatusProgressBar.Maximum = 100; this.StatusProgressBar.Value = percentage;
Here’s a work around to this:
this.StatusProgressBar.Use(p => { p.IsIndeterminate = false; p.Visibility = Visibility.Visible; p.Minimum = 0; p.Maximum = 100; p.Value = percentage;
});
Saves you repeatedly typing the same class instance or control name over and over again. It also makes code more readable since it clearly says that you are working with a progress bar control within the block. It you are setting properties of several controls one after another, it’s easier to read such code this way since you will have dedicated block for each control.
It’s a very simple one line function that does it:
public static void Use<T>(this T item, Action<T> work) { work(item); }
You could argue that you can just do this:
var p = this.StatusProgressBar; p.IsIndeterminate = false; p.Visibility = Visibility.Visible; p.Minimum = 0; p.Maximum = 100; p.Value = percentage;
But it’s not elegant. You are introducing a variable “p” in the local scope of the whole function. This goes against naming conventions. Morever, you can’t limit the scope of “p” within a certain place in the function.
Update: Previously I proposed a way to do it without generic extention method which was not so clean. Andy T posted this cleaner solution in comments.
No related posts.















by re: C# with keyword equivalent
21 Mar 2010 at 05:58
“You are introducing a variable p in the local scope of the whole function. This goes against naming conventions. Morever, you cant limit the scope of p within a certain place in the function.”
Yes you can! Just place the code in the brackets:
{
var p = var p = this.StatusProgressBar;
p.IsIndeterminate = false;
p.Visibility = Visibility.Visible;
}
by re: C# with keyword equivalent
21 Mar 2010 at 06:29
I have a few issues with your approach:
1. It's more to write.
2. If you have scoping troubles, you probably need another method.
3. Readability is always in the eye of the beholder
by re: C# with keyword equivalent
21 Mar 2010 at 07:03
What about (from memory):
var spb = new StatusProgressBar{
IsIndeterminate = false,
Visibility = Visibility.Visible,
Minimum = 0,
Maximum = 100,
Value = percentage
};
by re: C# with keyword equivalent
21 Mar 2010 at 09:29
very niiiiice!
by re: C# with keyword equivalent
21 Mar 2010 at 09:33
Definitely something the C# team could introduce.
by re: C# with keyword equivalent
22 Mar 2010 at 01:21
I'm sorry, the price of elegance is too much to pay IMHO: The method that uses generic is elegant, but the precompilator will generate as many function signature as used generic types. It's too much to me regarding the benefits of reading code.
I had to cope with that property setting issue for a long time, and I use to set local variables all the time and the new constructor's syntaxic sugar paul mentions, which sounds enough to me.
by C# with keyword equivalent
22 Mar 2010 at 02:37
Thank you for submitting this cool story – Trackback from DotNetShoutout
by re: C# with keyword equivalent
22 Mar 2010 at 04:50
Nice trick but i don't like it. Good code is mentioned by the number of WTF/s. Your code uses generic methods, extension methods, lambda's only to do 5 simple assignments. Only to be able to write 'p.' .
Sorry, I just don't buy it.
by C# with keyword equivalent
22 Mar 2010 at 04:53
Thank you for submitting this cool story – Trackback from iAwaaz-News-by-People
by Social comments and analytics for this post
22 Mar 2010 at 06:14
This post was mentioned on Twitter by JoeWirtley: Interesting way to simulate VB.NET With http://twurl.nl/lg3mgi
by re: C# with keyword equivalent
22 Mar 2010 at 07:51
Omar,
Using Lambda expression as a substitute of “with” statement is an overkill and impairs readability.
The second version (with “var p”) is a better approach.
by re: C# with keyword equivalent
22 Mar 2010 at 12:45
I know that some people like it, but can you explain why you use keyword 'this' ?
To me it is just added clutter that does not add any readability benefits.
You obviously want to reduce clutter so I am just curious why would you want to continue to use 'this'.
by re: C# with keyword equivalent
23 Mar 2010 at 09:07
I like it Omar! This is getting added to my extension methods list.
by re: C# with keyword equivalent
26 Mar 2010 at 06:11
As always, unique and interesting topics, thanks very much Omar.
by re: C# with keyword equivalent
26 Mar 2010 at 08:43
What about object initializer? I think it can help with this.
by Omar
06 Apr 2010 at 22:55
About object initializer, I am trying to set properties of an object which is already there. So, can’t use object initializer here.
About “this” keyword, I use to clearly indicate I am using stuff from current class, not from base class, not a static class either. For ex, if you write something like this:
SomeStuff.Open();
You don’t know by reading if SomeStuff is a static class or is it a property of current class. The “this” keyword clearly distinguishes this.
Pingback
by VB like with keywords in C# - Tanzim Saqib - Microsoft MVP, ASP.NET
09 Apr 2010 at 00:11
[...] following is a sample code Omar mentioned in his blog post. Unlike VB, in C# you have to mention the instance variable like every time whenever you intend to [...]
by Pat1679
11 Aug 2010 at 21:46
nice !
how about …..
var x = new XYZ{
property1 = 1,
property 2 = 2
.
.
.
.
propertyn = n};
by Omar AL Zabir
11 Aug 2010 at 21:51
That’s for new instances only. Mine is suitable for existing ones.