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. the key word "using" questiton?

the key word "using" questiton?

Scheduled Pinned Locked Moved C#
csharpgraphicsquestionlearning
12 Posts 6 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.
  • J Offline
    J Offline
    jinzhecheng
    wrote on last edited by
    #1

    Hello, Friends, // this is a piece of code from the book "Program Windows Form in C#" // it uses keyword "using" protected override void OnPaint(PaintEventArgs pe) { Graphics g = pe.Graphics; // after this , foreBush is disposed, so , how can it be used // in the next statement. // surprisingly. it works.:( using( Brush foreBrush = new SolidBrush(this.ForeColor) ) using( Brush backBrush = new SolidBrush(this.BackColor) ) { g.FillEllipse(foreBrush, this.ClientRectangle); ... } // Calling the base class OnPaint base.OnPaint(pe); }

    M J S C 4 Replies Last reply
    0
    • J jinzhecheng

      Hello, Friends, // this is a piece of code from the book "Program Windows Form in C#" // it uses keyword "using" protected override void OnPaint(PaintEventArgs pe) { Graphics g = pe.Graphics; // after this , foreBush is disposed, so , how can it be used // in the next statement. // surprisingly. it works.:( using( Brush foreBrush = new SolidBrush(this.ForeColor) ) using( Brush backBrush = new SolidBrush(this.BackColor) ) { g.FillEllipse(foreBrush, this.ClientRectangle); ... } // Calling the base class OnPaint base.OnPaint(pe); }

      M Offline
      M Offline
      MoustafaS
      wrote on last edited by
      #2

      It's disposed after all the statements in the using clause is executed.

      1 Reply Last reply
      0
      • J jinzhecheng

        Hello, Friends, // this is a piece of code from the book "Program Windows Form in C#" // it uses keyword "using" protected override void OnPaint(PaintEventArgs pe) { Graphics g = pe.Graphics; // after this , foreBush is disposed, so , how can it be used // in the next statement. // surprisingly. it works.:( using( Brush foreBrush = new SolidBrush(this.ForeColor) ) using( Brush backBrush = new SolidBrush(this.BackColor) ) { g.FillEllipse(foreBrush, this.ClientRectangle); ... } // Calling the base class OnPaint base.OnPaint(pe); }

        J Offline
        J Offline
        jinzhecheng
        wrote on last edited by
        #3

        // look at this: using( Brush foreBrush = new SolidBrush(this.ForeColor) ) // here foreBrush is disposed eh?? how come it can still be used? using( Brush backBrush = new SolidBrush(this.BackColor) )

        1 Reply Last reply
        0
        • J jinzhecheng

          Hello, Friends, // this is a piece of code from the book "Program Windows Form in C#" // it uses keyword "using" protected override void OnPaint(PaintEventArgs pe) { Graphics g = pe.Graphics; // after this , foreBush is disposed, so , how can it be used // in the next statement. // surprisingly. it works.:( using( Brush foreBrush = new SolidBrush(this.ForeColor) ) using( Brush backBrush = new SolidBrush(this.BackColor) ) { g.FillEllipse(foreBrush, this.ClientRectangle); ... } // Calling the base class OnPaint base.OnPaint(pe); }

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          The using statement is translated by the compiler from

          using(Brush foreBrush = new SolidBrush(this.ForeColor))
          {
          ...
          }

          to

          Brush foreBrush = null;
          try
          {
          foreBrush = new SolidBrush(this.ForeColor);
          ...
          }
          finally
          {
          if (foreBrush != null)
          ((IDisposable)foreBrush).Dispose();
          }

          foreBrush gets disposed in the finally block. Because you are using it within the block, it works. Your code will get translated into something like

          Brush foreBrush = null;
          try
          {
          foreBrush = new SolidBrush(this.ForeColor);
          Brush backBrush = null;
          try
          {
          backBrush = new SolidBrush(this.BackColor);
          ...
          }
          finally
          {
          if (backBrush != null)
          ((IDisposable)backBrush).Dispose();
          }
          }
          finally
          {
          if (foreBrush != null)
          ((IDisposable)foreBrush).Dispose();
          }

          Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          D J 2 Replies Last reply
          0
          • S S Senthil Kumar

            The using statement is translated by the compiler from

            using(Brush foreBrush = new SolidBrush(this.ForeColor))
            {
            ...
            }

            to

            Brush foreBrush = null;
            try
            {
            foreBrush = new SolidBrush(this.ForeColor);
            ...
            }
            finally
            {
            if (foreBrush != null)
            ((IDisposable)foreBrush).Dispose();
            }

            foreBrush gets disposed in the finally block. Because you are using it within the block, it works. Your code will get translated into something like

            Brush foreBrush = null;
            try
            {
            foreBrush = new SolidBrush(this.ForeColor);
            Brush backBrush = null;
            try
            {
            backBrush = new SolidBrush(this.BackColor);
            ...
            }
            finally
            {
            if (backBrush != null)
            ((IDisposable)backBrush).Dispose();
            }
            }
            finally
            {
            if (foreBrush != null)
            ((IDisposable)foreBrush).Dispose();
            }

            Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            D Offline
            D Offline
            DavidNohejl
            wrote on last edited by
            #5

            S. Senthil Kumar wrote: Brush foreBrush = null; try { foreBrush = new SolidBrush(this.ForeColor); ... } finally { if (foreBrush != null) ((IDisposable)foreBrush).Dispose(); } which begs a question, what if foreBush does not implement IDisposable? It will simply crash ( throw exception) ? David

            J 1 Reply Last reply
            0
            • S S Senthil Kumar

              The using statement is translated by the compiler from

              using(Brush foreBrush = new SolidBrush(this.ForeColor))
              {
              ...
              }

              to

              Brush foreBrush = null;
              try
              {
              foreBrush = new SolidBrush(this.ForeColor);
              ...
              }
              finally
              {
              if (foreBrush != null)
              ((IDisposable)foreBrush).Dispose();
              }

              foreBrush gets disposed in the finally block. Because you are using it within the block, it works. Your code will get translated into something like

              Brush foreBrush = null;
              try
              {
              foreBrush = new SolidBrush(this.ForeColor);
              Brush backBrush = null;
              try
              {
              backBrush = new SolidBrush(this.BackColor);
              ...
              }
              finally
              {
              if (backBrush != null)
              ((IDisposable)backBrush).Dispose();
              }
              }
              finally
              {
              if (foreBrush != null)
              ((IDisposable)foreBrush).Dispose();
              }

              Regards Senthil _____________________________ My Blog | My Articles | WinMacro

              J Offline
              J Offline
              jinzhecheng
              wrote on last edited by
              #6

              Hi , Senthil , does that mean, after foreBrush is disposed, if following code gona use it , then it will be constucted again ??

              S 1 Reply Last reply
              0
              • D DavidNohejl

                S. Senthil Kumar wrote: Brush foreBrush = null; try { foreBrush = new SolidBrush(this.ForeColor); ... } finally { if (foreBrush != null) ((IDisposable)foreBrush).Dispose(); } which begs a question, what if foreBush does not implement IDisposable? It will simply crash ( throw exception) ? David

                J Offline
                J Offline
                J4amieC
                wrote on last edited by
                #7

                Hence why only those objects which implement IDisposible can be used within a using block. In this case foreBrush is an instance of SolidBrush and that implements IDisposible

                D 1 Reply Last reply
                0
                • J J4amieC

                  Hence why only those objects which implement IDisposible can be used within a using block. In this case foreBrush is an instance of SolidBrush and that implements IDisposible

                  D Offline
                  D Offline
                  DavidNohejl
                  wrote on last edited by
                  #8

                  I know. I just thought that it could be like

                  finally
                  {
                  IDisposable thing;
                  if ((thing = foreBrush as IDisposable) != null)
                    thing.Dispose();
                  }
                  

                  OK, it's better how it is. David

                  S 1 Reply Last reply
                  0
                  • D DavidNohejl

                    I know. I just thought that it could be like

                    finally
                    {
                    IDisposable thing;
                    if ((thing = foreBrush as IDisposable) != null)
                      thing.Dispose();
                    }
                    

                    OK, it's better how it is. David

                    S Offline
                    S Offline
                    S Senthil Kumar
                    wrote on last edited by
                    #9

                    The code wouldn't compile if Brush didn't implement IDisposable. The compiler would complain that the type used in the "using" statement cannot be disposed, or something similar to that. So there's no question of a runtime crash. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                    D 1 Reply Last reply
                    0
                    • J jinzhecheng

                      Hi , Senthil , does that mean, after foreBrush is disposed, if following code gona use it , then it will be constucted again ??

                      S Offline
                      S Offline
                      S Senthil Kumar
                      wrote on last edited by
                      #10

                      No, it means that foreBrush will be disposed only at the end of its block. So

                      using (Brush foreBrush = new Brush())
                      using (Brush backBrush = new Brush())
                      {
                      }// dispose backBrush
                      // dispose foreBrush as the block ends here.

                      Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                      1 Reply Last reply
                      0
                      • S S Senthil Kumar

                        The code wouldn't compile if Brush didn't implement IDisposable. The compiler would complain that the type used in the "using" statement cannot be disposed, or something similar to that. So there's no question of a runtime crash. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                        D Offline
                        D Offline
                        DavidNohejl
                        wrote on last edited by
                        #11

                        S. Senthil Kumar wrote: The code wouldn't compile That's exactly the answer I was looking for. Thanks. David

                        1 Reply Last reply
                        0
                        • J jinzhecheng

                          Hello, Friends, // this is a piece of code from the book "Program Windows Form in C#" // it uses keyword "using" protected override void OnPaint(PaintEventArgs pe) { Graphics g = pe.Graphics; // after this , foreBush is disposed, so , how can it be used // in the next statement. // surprisingly. it works.:( using( Brush foreBrush = new SolidBrush(this.ForeColor) ) using( Brush backBrush = new SolidBrush(this.BackColor) ) { g.FillEllipse(foreBrush, this.ClientRectangle); ... } // Calling the base class OnPaint base.OnPaint(pe); }

                          C Offline
                          C Offline
                          Carsten Zeumer
                          wrote on last edited by
                          #12

                          there is no ";" after the using( Brush foreBrush = new SolidBrush(this.ForeColor) ). it behaves like the if clause. you can use it like this: if (x == true) if (y==true) { ... code in block ... } or like this: if (x == true) { if (y==true) { ... code in block ... } } if there would be a semicolon right after the using statement your assumption that the foreBrush should be disposed would be true. /cadi

                          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