Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

C# equivalent of VB's With keyword

5.00/5 (6 votes)
27 May 2011CPOL 13K  
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)