Click here to Skip to main content
15,875,017 members
Articles / Programming Languages / C#
Tip/Trick

C# equivalent of VB's With keyword

Rate me:
Please Sign up or sign in to vote.
4.84/5 (49 votes)
27 May 2011CPOL 139.8K   23   18
VB has a With keyword that you can use to save typing same variables name over and over again. Here's a similar workaround for C#
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 workaround to this:
this.StatusProgressBar.Use(p =>
{
  p.IsIndeterminate = false;
  p.Visibility = Visibility.Visible;
  p.Minimum = 0;
  p.Maximum = 100;
  p.Value = percentage;
});

This 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 extension method that enables 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect BT, UK (ex British Telecom)
United Kingdom United Kingdom

Comments and Discussions

 
GeneralI gave it a 4 because it's a well-performed trick. I would n... Pin
dmjm-h25-May-11 6:26
dmjm-h25-May-11 6:26 
GeneralThanks mate, I've been looking for this for quite a while no... Pin
Steven O23-May-11 18:49
Steven O23-May-11 18:49 
GeneralOmar, I like it, it has my vote of 5. I also see there have... Pin
FDW17-May-11 23:44
FDW17-May-11 23:44 
GeneralRe: I am curious to know why the low votes as well. May be I nee... Pin
Omar Al Zabir18-May-11 0:38
Omar Al Zabir18-May-11 0:38 
GeneralRe: i could be wrong but i think vb.net's "with" is a compiler f... Pin
HaBiX19-May-11 23:06
HaBiX19-May-11 23:06 
QuestionThoughts Pin
PIEBALDconsult18-Jan-12 11:09
mvePIEBALDconsult18-Jan-12 11:09 
I've never liked the with statement; not in VB and not in Pascal before that. It seems silly and useless. And this tip strikes me as unreadable and inefficient. But at least the alternatives are easier to read and you can easily have them refer to multiple instances.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.