C# with keyword equivalent

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.

20 comments

  1. re: C# with keyword equivalent says:

    “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;

    }

  2. re: C# with keyword equivalent says:

    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

  3. re: C# with keyword equivalent says:

    What about (from memory):

    var spb = new StatusProgressBar{

    IsIndeterminate = false,

    Visibility = Visibility.Visible,

    Minimum = 0,

    Maximum = 100,

    Value = percentage

    };

  4. re: C# with keyword equivalent says:

    very niiiiice! :)

  5. re: C# with keyword equivalent says:

    Definitely something the C# team could introduce.

  6. re: C# with keyword equivalent says:

    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.

  7. C# with keyword equivalent says:

    Thank you for submitting this cool story – Trackback from DotNetShoutout

  8. re: C# with keyword equivalent says:

    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.

  9. C# with keyword equivalent says:

    Thank you for submitting this cool story – Trackback from iAwaaz-News-by-People

  10. Social comments and analytics for this post says:

    This post was mentioned on Twitter by JoeWirtley: Interesting way to simulate VB.NET With http://twurl.nl/lg3mgi

  11. re: C# with keyword equivalent says:

    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.

  12. re: C# with keyword equivalent says:

    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'.

  13. re: C# with keyword equivalent says:

    I like it Omar! This is getting added to my extension methods list.

  14. re: C# with keyword equivalent says:

    As always, unique and interesting topics, thanks very much Omar.

  15. re: C# with keyword equivalent says:

    What about object initializer? I think it can help with this.

  16. Omar says:

    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.

  17. […] 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 […]

  18. Pat1679 says:

    nice !
    how about …..

    var x = new XYZ{

    property1 = 1,
    property 2 = 2
    .
    .
    .
    .
    propertyn = n};

  19. Hi,

    Sorry for spamming you, and please free to throw heavy objects at me if you’re mad about this. I saw your articles on CodeProject, and enjoyed them a lot. And I thought that maybe you could post a review of my project, and online .Net IDE, called Chpokk. You can find it here: http://chpokk.apphb.com/. Maybe you’ll even find it useful, in which case I’ll be happy to give you a free membership.

    Thanks!
    artem

Leave a Reply

Your email address will not be published. Required fields are marked *