On 29-Mar-11 18:12, Peter Duniho wrote:
> On 3/29/11 8:39 AM, Willem van Rumpt wrote:
>
>>> protected override void Dispose(bool disposing)
>>> {
>>> 2 if (disposing)
>>> 2 {
>>> 2 theImage.Dispose();
>>> 2 }
>>>
>>>
>>> if (disposing&& (components != null))
>>> {
>>> components.Dispose();
>>> }
>>>
>>> base.Dispose(disposing);
>>> }
>>>
>>
>> This is more like it, although you might want include a null check for
>> "theImage".
>
> The code as written cannot find the variable set to null. Of course, if
> there is other code that does set the variable to null, then checking
> for null would be useful. But otherwise, a check for null is just
> wasted, misleading code (misleading, because its mere presence implies
> that the variable _could_ be null).
>
I didn't see the constructor and the overriden Dispose as being
connected. But if they are, then yes, it can't be null, and I agree
completely.
>
> And if the object doesn't have to have a lifetime equal to that of the
> form, the question remains: why would it be an instance field in the
> first place?
>
It might be initialized outside the constructor, defaulting to null if
never used. Plenty of possible scenarios.
--
Willem van Rumpt
|