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. Scope of variable

Scope of variable

Scheduled Pinned Locked Moved C#
csharpc++question
8 Posts 5 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.
  • Z Offline
    Z Offline
    zafersavas
    wrote on last edited by
    #1

    I know this is a simple question but I would like to know How and Why this works; Lets say I have two forms : Form1 and Form2 where Form1 is the main form. I am creating a Form2 variable by clicking a button in Form1 as seen in the code piece below.

    private void button1_Click(object sender, EventArgs e)
    {
    Form2 myForm2 = new Form2();
    myForm2.Show();
    }

    Why am I able to see form2. Because myForm2 is a local variable and just after it is shown, the function button1_Click terminates. So I mustn't be able to see Form2. NOTE-1 : If you try the same with MS VC++ you will not be able to see Form2. NOTE-2 : I know in .NET variables are not deleted when they are out out of scope. Garbage collector does it. Bu in an other function I tried GC.Collect(); which forces the garbage collector but Form2 still remains, it is not deleted. Regards

    L P N 3 Replies Last reply
    0
    • Z zafersavas

      I know this is a simple question but I would like to know How and Why this works; Lets say I have two forms : Form1 and Form2 where Form1 is the main form. I am creating a Form2 variable by clicking a button in Form1 as seen in the code piece below.

      private void button1_Click(object sender, EventArgs e)
      {
      Form2 myForm2 = new Form2();
      myForm2.Show();
      }

      Why am I able to see form2. Because myForm2 is a local variable and just after it is shown, the function button1_Click terminates. So I mustn't be able to see Form2. NOTE-1 : If you try the same with MS VC++ you will not be able to see Form2. NOTE-2 : I know in .NET variables are not deleted when they are out out of scope. Garbage collector does it. Bu in an other function I tried GC.Collect(); which forces the garbage collector but Form2 still remains, it is not deleted. Regards

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, a variable is visible ("in scope") as long as you are within the first pair of brackets {} that encloses its declaration, so myForm2 lives only inside your button1_Click method. The Form it refers to is an object, calling Show() makes it visible AND keeps it alive, even when button1_Click returns and hence variable myForm2 fades away. Once visible, the Form does not need the variable, it has its own life. You would have to close the Form somehow (by clicking a Close box, or by calling the Close() method) to end its useful life and make it collectable. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Voting for dummies? No thanks. X|


      Z 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, a variable is visible ("in scope") as long as you are within the first pair of brackets {} that encloses its declaration, so myForm2 lives only inside your button1_Click method. The Form it refers to is an object, calling Show() makes it visible AND keeps it alive, even when button1_Click returns and hence variable myForm2 fades away. Once visible, the Form does not need the variable, it has its own life. You would have to close the Form somehow (by clicking a Close box, or by calling the Close() method) to end its useful life and make it collectable. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Voting for dummies? No thanks. X|


        Z Offline
        Z Offline
        zafersavas
        wrote on last edited by
        #3

        Thats the point; "Once visible, the Form does not need the variable". I just can't understand why and how?, sorry :) But thats not the case in MS VC++. Once you create a dialog class and if the object is out of scope, the form is also deleted. Right? There are strange things going behind c# :)

        L F 2 Replies Last reply
        0
        • Z zafersavas

          Thats the point; "Once visible, the Form does not need the variable". I just can't understand why and how?, sorry :) But thats not the case in MS VC++. Once you create a dialog class and if the object is out of scope, the form is also deleted. Right? There are strange things going behind c# :)

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          zafersavas wrote:

          There are strange things going behind c#

          Oh no, if anything is strange it is in the C++ world. A visible Form remains alive without a reference; same is true for a file you create on disk, it does not get deleted when your file variable vanishes. BTW dialogs get shown using ShowDialog() which does not return until the Dialog gets closed. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Voting for dummies? No thanks. X|


          1 Reply Last reply
          0
          • Z zafersavas

            I know this is a simple question but I would like to know How and Why this works; Lets say I have two forms : Form1 and Form2 where Form1 is the main form. I am creating a Form2 variable by clicking a button in Form1 as seen in the code piece below.

            private void button1_Click(object sender, EventArgs e)
            {
            Form2 myForm2 = new Form2();
            myForm2.Show();
            }

            Why am I able to see form2. Because myForm2 is a local variable and just after it is shown, the function button1_Click terminates. So I mustn't be able to see Form2. NOTE-1 : If you try the same with MS VC++ you will not be able to see Form2. NOTE-2 : I know in .NET variables are not deleted when they are out out of scope. Garbage collector does it. Bu in an other function I tried GC.Collect(); which forces the garbage collector but Form2 still remains, it is not deleted. Regards

            P Online
            P Online
            PIEBALDconsult
            wrote on last edited by
            #5

            The variable contains a reference to the Form instance, a copy of that reference gets passed on to the Window Manager (or whatever), at which point your app and the whatever both refer to the instance, even when your reference is destroyed the whatever maintains its reference.

            1 Reply Last reply
            0
            • Z zafersavas

              Thats the point; "Once visible, the Form does not need the variable". I just can't understand why and how?, sorry :) But thats not the case in MS VC++. Once you create a dialog class and if the object is out of scope, the form is also deleted. Right? There are strange things going behind c# :)

              F Offline
              F Offline
              Frank Horn
              wrote on last edited by
              #6

              That's because the Form class is just a wrapper in the .Net runtime environment to manage a Windows API window handle. Creating a form and calling its Show method creates a window handle, and the window handle will remain until it is released (which happens when the form's Dispose method is called, be it from code or by Garbage Collecgtion). All these .Net classes handling windows API (or COM interop, which is even worse) have been designed to make life easy as long as you don't think about it too much. Soon as you start thinking it looks illogical and only reveals its logic when you read up on the System.Runtime.InteropServices namespace. Being an old C hand you'll know much better than I what happens to window handles and things when they're not released. All I know is that when I detect a memory leak I have to look into the interop hell unless I can find an easy way (within .Net) to clean things up. That's twe price we pay for using a comfortable top level language like C# instead of ATL and MFC.

              1 Reply Last reply
              0
              • Z zafersavas

                I know this is a simple question but I would like to know How and Why this works; Lets say I have two forms : Form1 and Form2 where Form1 is the main form. I am creating a Form2 variable by clicking a button in Form1 as seen in the code piece below.

                private void button1_Click(object sender, EventArgs e)
                {
                Form2 myForm2 = new Form2();
                myForm2.Show();
                }

                Why am I able to see form2. Because myForm2 is a local variable and just after it is shown, the function button1_Click terminates. So I mustn't be able to see Form2. NOTE-1 : If you try the same with MS VC++ you will not be able to see Form2. NOTE-2 : I know in .NET variables are not deleted when they are out out of scope. Garbage collector does it. Bu in an other function I tried GC.Collect(); which forces the garbage collector but Form2 still remains, it is not deleted. Regards

                N Offline
                N Offline
                Nirandas
                wrote on last edited by
                #7

                When ever you create a form and show it, a reference to the form is added internaly. There is a property of Application Object named "OpenForms" which returns a read only FormsCollection containing all the forms opened by the application. Since a reference is held to the form object even though your local variable is collected by gc, the form does not get collected. You can access the form through that collection and call the close method on it to close the form. Then the form is removed from the OpenForms collection and will be collected by the gc. HTH

                Nirandas, a developer from India. http://www.nirandas.com

                Z 1 Reply Last reply
                0
                • N Nirandas

                  When ever you create a form and show it, a reference to the form is added internaly. There is a property of Application Object named "OpenForms" which returns a read only FormsCollection containing all the forms opened by the application. Since a reference is held to the form object even though your local variable is collected by gc, the form does not get collected. You can access the form through that collection and call the close method on it to close the form. Then the form is removed from the OpenForms collection and will be collected by the gc. HTH

                  Nirandas, a developer from India. http://www.nirandas.com

                  Z Offline
                  Z Offline
                  zafersavas
                  wrote on last edited by
                  #8

                  Thanks. I understand now; You can reach to the form by Application.OpenForms[1]. This makes sense :)

                  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