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);
}
Read more: Codeproject
0 comments:
Post a Comment