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

有帮助吗?

解决方案

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);
}

其他提示

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?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top