65.9K
CodeProject is changing. Read more.
Home

C# equivalent of VB's With keyword

starIconstarIconstarIconstarIconstarIcon

5.00/5 (6 votes)

May 26, 2011

CPOL
viewsIcon

13021

You can limit the scope of 'p' inside your function:private void Whatever() { DoStuffHere(); // 'p' is not in scope. { var p = this.StatusProgressBar; // 'p' is in scope. p.IsIndeterminate = false; p.[etc] } // 'p' is not in...

You can limit the scope of 'p' inside your function:
private void Whatever() {
    DoStuffHere();
    // 'p' is not in scope.

    {
        var p = this.StatusProgressBar;
        // 'p' is in scope.

        p.IsIndeterminate = false;
        p.[etc]
    }

    // 'p' is not in scope.
    DoMoreHere();
}
Notice the open and close braces - they create a scope that 'p' will not be leaked from. Personally, I like your idea better - but it *is* possible to limit scope within a function.