Click here to Skip to main content
Click here to Skip to main content

C# equivalent of VB's With keyword

By , 27 May 2011
 
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)

About the Author

Omar Al Zabir
Architect BT, UK (ex British Telecom)
United Kingdom United Kingdom
Member

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberMichael Haephrati19 Sep '12 - 6:08 
GeneralIs it really necessary?memberlewax0010 Apr '12 - 8:12 
QuestionI prefer the alternativememberGParkings6 Mar '12 - 23:54 
GeneralRe: It's true that it'll be slower as you're creating a delegate...memberRiz Thon23 May '11 - 20:22 
GeneralReason for my vote of 5 Good alternative.memberProEnggSoft24 Feb '12 - 19:43 
GeneralReason for my vote of 5 Good summary. Sad that C# did not im...memberRoy from Detroit21 Feb '12 - 4:12 
GeneralReason for my vote of 3 performance impact for syntactic sug...memberjohannesnestler15 Feb '12 - 4:09 
GeneralMuch less efficient than the VB version, as it introduces an...memberIan Shlasko1 Jun '11 - 3:11 
GeneralGood and safe option for those who prefer such coding style....memberAll Time Programming30 May '11 - 20:52 
GeneralInteresting though I'd prefer to use the alternate (and have...memberdigital man26 May '11 - 4:48 
GeneralI gave it a 4 because it's a well-performed trick. I would n...memberdmjm-h25 May '11 - 6:26 
GeneralThanks mate, I've been looking for this for quite a while no...memberSteven O23 May '11 - 18:49 
GeneralOmar, I like it, it has my vote of 5. I also see there have...memberFDW17 May '11 - 23:44 
GeneralRe: I am curious to know why the low votes as well. May be I nee...memberOmar Al Zabir18 May '11 - 0:38 
GeneralRe: i could be wrong but i think vb.net's "with" is a compiler f...memberHaBiX19 May '11 - 23:06 
QuestionThoughtsmemberPIEBALDconsult18 Jan '12 - 11:09 

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

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