Here’s an interesting utility class, a scoped value. This allows you to have a member variable that gets changed for a time, and then automatically restored back to the original value.

Looked at another way, a Scoped is a member variable where changes are not permanent. Instead, changes are kept during a particular scope of execution, then reverted.

When is this useful?

One sample use is for the mouse cursor - with a suitably configured Scoped you can simply change the cursor to an hour glass during a process and then have it automatically restored again afterwards.

using (screenCursor.As(Cursors.HourGlass))
{
    // Long running action
}

Contrast this with the code often written for this situation:

Control.Cursor = Cursors.HourGlass;
try
{
    // Long running action
}
finally
{
    Control.Cursor = Cursors.Default;
}

One major issue with this code is that it doesn’t support nesting - when this particular block completes, the cursor is restored to its default value, even if this block has been called as a part of processing a larger piece of work. Of course, this can be easily addressed by caching the existing value in a temporary variable, but this increases the amount of boilerplate code required.

Using a Scoped takes care of these details, leaving you to focus on the core logic that needs your attention.

Setup is relatively straightforward, just create a Scoped member variable and hook up a handler to the ValueChanged event.

Here’s a full example …

public class ExampleForm : Form
{
    public ExampleForm()
    {
        mScreenCursor = new Scoped<Cursor>(Cursors.Default);
        mScreenCursor.ValueChanged += UpdateCursor;
    }

    private void UpdateCursor(
        object sender,
        ValueChangedEventArgs<Cursor> args)
    {
        Cursor = args.Value;
    }

    private readonly Scoped<Cursor> mScreenCursor;
}

Comments

blog comments powered by Disqus

Download

Scoped

Next Post
Synergy  20 Jun 2009
Prior Post
Quote of the Day  14 Jun 2009
Related Posts
Browsers and WSL  31 Mar 2024
Factory methods and functions  05 Mar 2023
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
A bash puzzle  25 Jun 2022
Improve your troubleshooting by aggregating errors  11 Jun 2022
Improve your troubleshooting by wrapping errors  28 May 2022
Keep your promises  14 May 2022
Archives
June 2009
2009