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 thoughts on “C# with keyword equivalent”

  1. “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. 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. What about (from memory):

    var spb = new StatusProgressBar{

    IsIndeterminate = false,

    Visibility = Visibility.Visible,

    Minimum = 0,

    Maximum = 100,

    Value = percentage

    };

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

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

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

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

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

  9. 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 to C# with keyword equivalent Cancel reply