Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. inelegance

inelegance

Scheduled Pinned Locked Moved C#
question
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TuringTest1
    wrote on last edited by
    #1

    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!

    P 1 Reply Last reply
    0
    • T TuringTest1

      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!

      P Offline
      P Offline
      Philip Fitzsimons
      wrote on last edited by
      #2

      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."

      L 1 Reply Last reply
      0
      • P Philip Fitzsimons

        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."

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        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.

        P 1 Reply Last reply
        0
        • L leppie

          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.

          P Offline
          P Offline
          Philip Fitzsimons
          wrote on last edited by
          #4

          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."

          T L 2 Replies Last reply
          0
          • P Philip Fitzsimons

            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."

            T Offline
            T Offline
            TuringTest1
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            • P Philip Fitzsimons

              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."

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              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.

              T 1 Reply Last reply
              0
              • L leppie

                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.

                T Offline
                T Offline
                TuringTest1
                wrote on last edited by
                #7

                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!

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups