Question

I ran a static code analysis tool on our tool and looking at its results the code below was one of the things it was talking about:

    SpreadSnapshot oSnap = new SpreadSnapshot();
    using (oSnap.SetRowCol(fpSpread, row, col))
    {
        SpreadSetComboBox(fpSpread, list, displayProperty);
    }

So I changed it to the code below and it fixed the error that the tool was talking about:

    using (SpreadSnapshot oSnap = new SpreadSnapshot())
    {
        oSnap.SetRowCol(fpSpread, row, col);
        SpreadSetComboBox(fpSpread, list, displayProperty);
    }

So in your opinion Which style of coding do you think is more appropriate and less error-prone?

Thanks

Was it helpful?

Solution

The two mean completely different things, unless SetRowCol returns this at the end. In the first, you're disposing the results of SetRowCol. In the second, you're disposing the SpreadSnapshot.

If both are disposable, you should do a using for both:

using (SpreadSnapshot oSnap = new SpreadSnapshot())
using (oSnap.SetRowCol(fpSpread, row, col))
{
    SpreadSetComboBox(fpSpread, list, displayProperty);
}

OTHER TIPS

The latter - it ensures that you don't end up using oSnap after the using statement.

Aside from anything else, it would be pretty odd for SetRowCol to return something disposable... what would that even mean?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top