문제

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