Click here to Skip to main content
13,797,606 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


23 bookmarked
Posted 17 May 2011
Licenced CPOL

C# equivalent of VB's With keyword

, 27 May 2011
Rate this:
Please Sign up or sign in to vote.
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)

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.


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


About the Author

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
RAZblack15-Mar-15 13:43
memberRAZblack15-Mar-15 13:43 
GeneralMy vote of 5 Pin
Michael Haephrati19-Sep-12 7:08
mvpMichael Haephrati19-Sep-12 7:08 
GeneralIs it really necessary? Pin
lewax0010-Apr-12 9:12
memberlewax0010-Apr-12 9:12 
QuestionI prefer the alternative Pin
GParkings7-Mar-12 0:54
memberGParkings7-Mar-12 0:54 
GeneralRe: It's true that it'll be slower as you're creating a delegate... Pin
Riz Thon23-May-11 21:22
professionalRiz Thon23-May-11 21:22 
GeneralReason for my vote of 5 Good alternative. Pin
ProEnggSoft24-Feb-12 20:43
memberProEnggSoft24-Feb-12 20:43 
GeneralReason for my vote of 5 Good summary. Sad that C# did not im... Pin
Roy from Detroit21-Feb-12 5:12
memberRoy from Detroit21-Feb-12 5:12 
GeneralReason for my vote of 3 performance impact for syntactic sug... Pin
johannesnestler15-Feb-12 5:09
memberjohannesnestler15-Feb-12 5:09 
GeneralMuch less efficient than the VB version, as it introduces an... Pin
Ian Shlasko1-Jun-11 4:11
memberIan Shlasko1-Jun-11 4:11 
GeneralGood and safe option for those who prefer such coding style.... Pin
All Time Programming30-May-11 21:52
memberAll Time Programming30-May-11 21:52 
GeneralInteresting though I'd prefer to use the alternate (and have... Pin
R. Giskard Reventlov26-May-11 5:48
professionalR. Giskard Reventlov26-May-11 5:48 
GeneralI gave it a 4 because it's a well-performed trick. I would n... Pin
dmjm-h25-May-11 7:26
memberdmjm-h25-May-11 7:26 
GeneralThanks mate, I've been looking for this for quite a while no... Pin
Steven O23-May-11 19:49
memberSteven O23-May-11 19:49 
GeneralOmar, I like it, it has my vote of 5. I also see there have... Pin
FDW18-May-11 0:44
memberFDW18-May-11 0:44 
GeneralRe: I am curious to know why the low votes as well. May be I nee... Pin
Omar Al Zabir18-May-11 1:38
memberOmar Al Zabir18-May-11 1:38 
GeneralRe: i could be wrong but i think's "with" is a compiler f... Pin
HaBiX20-May-11 0:06
memberHaBiX20-May-11 0:06 
QuestionThoughts Pin
PIEBALDconsult18-Jan-12 12:09
protectorPIEBALDconsult18-Jan-12 12:09 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.181207.3 | Last Updated 27 May 2011
Article Copyright 2011 by Omar Al Zabir
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid