inelegance
-
The snippet below seems to me to reek of inelegance, yet i have no better solution. i can't manage with using {} because the pictureBox barfs if i dispose the image too soon. Is there a better way?? Image disposeOfSoon = pictureBox.Image; pictureBox.Image = newImage; if (disposeOfSoon != null) disposeOfSoon.Dispose (); ________________________________________ Gosh, it would be awful pleas'n, to reason out the reason, for things I can't explain. Then perhaps I'd deserve ya, and be even worthy of ya.. if I only had a brain!
-
The snippet below seems to me to reek of inelegance, yet i have no better solution. i can't manage with using {} because the pictureBox barfs if i dispose the image too soon. Is there a better way?? Image disposeOfSoon = pictureBox.Image; pictureBox.Image = newImage; if (disposeOfSoon != null) disposeOfSoon.Dispose (); ________________________________________ Gosh, it would be awful pleas'n, to reason out the reason, for things I can't explain. Then perhaps I'd deserve ya, and be even worthy of ya.. if I only had a brain!
Image disposeOfSoon = pictureBox.Image; using (disposeOfSoon) { // use disposeOfSoon pictureBox.Image = newImage; } // compiler will call Dispose on disposeOfSoon
"When the only tool you have is a hammer, a sore thumb you will have."
-
Image disposeOfSoon = pictureBox.Image; using (disposeOfSoon) { // use disposeOfSoon pictureBox.Image = newImage; } // compiler will call Dispose on disposeOfSoon
"When the only tool you have is a hammer, a sore thumb you will have."
I'm not sure if that works, but if it does, its totally ludicrous!!! 1. IMO the variable shouldnt leave the using block. 2. No object was created, only referenced. leppie::AllocCPArticle("Zee blog");
Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it. -
I'm not sure if that works, but if it does, its totally ludicrous!!! 1. IMO the variable shouldnt leave the using block. 2. No object was created, only referenced. leppie::AllocCPArticle("Zee blog");
Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.I have to be honest that I could not see the point myself with the original code. So I guessed its not real and just an example. in terms of your points: 1. the variable does not leave the using block... 2. calling dispose on an object which is pointing at a image works, as all you are doing is asking the image object to call dispose - which is worth doing if you don't want it anymore. hope this makes sense, or else I've misunderstood your points :(
"When the only tool you have is a hammer, a sore thumb you will have."
-
I have to be honest that I could not see the point myself with the original code. So I guessed its not real and just an example. in terms of your points: 1. the variable does not leave the using block... 2. calling dispose on an object which is pointing at a image works, as all you are doing is asking the image object to call dispose - which is worth doing if you don't want it anymore. hope this makes sense, or else I've misunderstood your points :(
"When the only tool you have is a hammer, a sore thumb you will have."
The code is real, it describes a common situation: a picture box displays an image for some unspecified duration, then in response to some event the image should change. AFAIK this requires the soon-to-be-old current image be pointed to, the new image be displayed, and the now-old-current image be disposed. Surely this is a common problem. Nes pas? ________________________________________ Gosh, it would be awful pleas'n, to reason out the reason, for things I can't explain. Then perhaps I'd deserve ya, and be even worthy of ya.. if I only had a brain!
-
I have to be honest that I could not see the point myself with the original code. So I guessed its not real and just an example. in terms of your points: 1. the variable does not leave the using block... 2. calling dispose on an object which is pointing at a image works, as all you are doing is asking the image object to call dispose - which is worth doing if you don't want it anymore. hope this makes sense, or else I've misunderstood your points :(
"When the only tool you have is a hammer, a sore thumb you will have."
Philip Fitzsimons wrote: the variable does not leave the using block... What I meant to say was the variable was enclosed, iow declared too. Thus the compiler can pickup a problem readily. Philip Fitzsimons wrote: calling dispose on an object which is pointing at a image works, as all you are doing is asking the image object to call dispose - which is worth doing if you don't want it anymore. Its all and well, but not if it someone elses object that mite need to exist after the method returns. Refer to above. IMO the object should be sandboxed in the scope. leppie::AllocCPArticle("Zee blog");
Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it. -
Philip Fitzsimons wrote: the variable does not leave the using block... What I meant to say was the variable was enclosed, iow declared too. Thus the compiler can pickup a problem readily. Philip Fitzsimons wrote: calling dispose on an object which is pointing at a image works, as all you are doing is asking the image object to call dispose - which is worth doing if you don't want it anymore. Its all and well, but not if it someone elses object that mite need to exist after the method returns. Refer to above. IMO the object should be sandboxed in the scope. leppie::AllocCPArticle("Zee blog");
Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.i agree in principle with what you're saying, that's why i wrote the post. the problem is that there isn't any good local "scope" for the image, once the pictureBox gets it it should belong to the pictureBox. perhaps i should write PictureBoxEx.. (yecch!!) btw the snippet provided by Philip Fitzsimons "looks" kinda wierd but works great! if we could get EricGu to rename the keyword "using" to "noLongerUsing" the snippet would read: Image tmp; noLongerUsing (tmp = pictureBox.Image) {pictureBox.Image = aBrandNewImage;} :) ________________________________________ Gosh, it would be awful pleas'n, to reason out the reason, for things I can't explain. Then perhaps I'd deserve ya, and be even worthy of ya.. if I only had a brain!