Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# equivalent of VB With - Endwith
Message
From
12/04/2012 03:48:52
 
 
To
11/04/2012 21:46:28
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01541144
Message ID:
01541154
Views:
61
>Did not know this ( used a variable but didn't think of subscope )
>
>Thanks to this article in Code Project
>http://www.codeproject.com/Tips/202369/C-equivalent-of-VB-s-With-keyword
>
>You are introducing a variable “p” in the local scope of the whole function :s
>not really.. you know you can use brackets anywhere inside the code to define subscopes, don't you:
>
>
>{
>var p = this.StatusProgressBar;
>p.IsIndeterminate = false;
>p.Visibility = Visibility.Visible;
>p.Minimum = 0;
>p.Maximum = 100;
>p.Value = percentage;
>}
>
Of course the argument most often put forward by the VB camp for WITH....ENDWITH is that it reduces the amount of typing required. Which begs the question of why, if that is important, they chose VB in the first place (g,d&r). Here's a clever alternative: http://stackoverflow.com/questions/1063429/equivalence-of-with-end-with-in-c.

Essentially:
public static void With<T>(this T _object, Action<T> _action) { _action(_object); }
Which would allow:
this.StatusProgressBar.With(p=>
{
    p.Isdeterminate = false;
    //etc
}
);
Then again it's no more readable than the locally scoped variable version and is probably less efficient (although potentiallly much more flexible)
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform