Results 1 to 16 of 16

Thread: [RESOLVED] variables in a sealed class retain value after form close

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Resolved [RESOLVED] variables in a sealed class retain value after form close

    I have a internal sealed class called gVariables in a Class Library (component) in .NET6, why the variables (e.g. g_ZoomFactor) retain value after my frmDemo1 closed? I open frmDemo2.cs with another MyUC, why frmDemo2's g_ZoomFactor inherits frmDemo1.MyUC1's value of g_ZoomFactor instead of initial value 1F.

    Code:
    namespace MyWorkSpace
    {
        internal sealed class gVariables
       {
           //...
           public static float g_ZoomFactor = 1F;
          //...
        }
    }
    
    namespace MyWorkSpace
    {
         public partial class MyUC: Control
        {
            private float m_sngVisualZoomFactor;
            public float Zoom
            {
                get
                {
                    return m_sngVisualZoomFactor;
                }
                set
                {
                    if (m_sngVisualZoomFactor != value && (value >= MINZOOMFACTOR && value <= MAXZOOMFACTOR))
                    {
                        m_sngVisualZoomFactor = value;
                        gVariables.g_ZoomFactor = value;
                        ZoomScalings();
                        RedrawControl();
                    }                
                }
            }
    
         }
    }
    Last edited by DaveDavis; Sep 8th, 2022 at 11:54 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: variables in a sealed class retain value after form close

    You have declared the field 'static', which explicitly means that there is one value for the entire class, not different values for different instances. If you want the latter then you need to not declare the field static and create separate instances of the class where you want each different value.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: variables in a sealed class retain value after form close

    Quote Originally Posted by jmcilhinney View Post
    You have declared the field 'static', which explicitly means that there is one value for the entire class, not different values for different instances. If you want the latter then you need to not declare the field static and create separate instances of the class where you want each different value.
    But the class is destroyed after frmDemo1 close, right? why the value still retain and pass to frmDemo2?
    MyUC is a component UserControl (MyUC.dll).
    In the demo project, there're three forms, frmMain, frmDemo1.cs and frmDemo2.cs.
    On frmDemo1.cs, I drag the MyUC from toolbox to frmDemo1;
    On frmDemo2.cs, I drag the MyUC from toolbox to frmDemo2;

    there's a frmMain.cs:

    Code:
     private void mnuDemo1_Click(object sender, EventArgs e)
            {
                frmDemo1 Demo1 = new frmDemo1();
                Demo1.ShowDialog();
                Demo1.Dispose();
            }
    
            private void mnuDemo2_Click(object sender, EventArgs e)
            {
                frmDemo2 Demo2 = new frmDemo2();
                Demo2.ShowDialog();
                Demo2.Dispose();
            }
    Last edited by DaveDavis; Sep 9th, 2022 at 12:56 AM.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: variables in a sealed class retain value after form close

    I can re-initialize the value in Sub New, but I am wondering why those static variables' value pass to another form?

    Code:
    Namespace MyWorkSpace
        Public Partial Class MyUC
            Inherits Control
           Public Sub New()
               gVariables.g_ZoomFactor = 1F
           End Sub
    
        End Class    
    End Namespace
    Last edited by DaveDavis; Sep 9th, 2022 at 01:36 AM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: variables in a sealed class retain value after form close

    Quote Originally Posted by DaveDavis View Post
    But the class is destroyed after frmDemo1 close, right?
    No, wrong. The class is never destroyed. Instances of a class are "destroyed" but the class itself is not.
    Quote Originally Posted by DaveDavis View Post
    why the value still retain and pass to frmDemo2?
    I already told you why and what to do about it.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: variables in a sealed class retain value after form close

    Quote Originally Posted by jmcilhinney View Post
    This is simply asking the same question that I have already answered in the C# forum. As I have already told you, the whole point of static/Shared fields is that there is one and only one value for the class as a whole. If you want different values for different instances then use instance fields and create instances. You're wondering why static/Shared fields do exactly what they exist to do.
    I understand what you said. My wondering is : frmDemo1 is already closed, the instance of MyUC on frmdemo1 also destroyed, now we are on frmDemo2 and another instance of MyUC, why the static value pass to frmDemo2?

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: variables in a sealed class retain value after form close

    Quote Originally Posted by jmcilhinney View Post
    No, wrong. The class is never destroyed. Instances of a class are "destroyed" but the class itself is not.
    OK, then it explains why static valve being passed. Do you have MSDN links about this?

  8. #8
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: variables in a sealed class retain value after form close

    Quote Originally Posted by DaveDavis View Post
    OK, then it explains why static valve being passed. Do you have MSDN links about this?
    https://docs.microsoft.com/en-us/dot...eywords/static

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: variables in a sealed class retain value after form close

    Quote Originally Posted by DaveDavis View Post
    I understand what you said.
    Quote Originally Posted by DaveDavis View Post
    why the static value pass to frmDemo2?
    Obviously you don't understand what I said or you wouldn't be asking the same question yet again. I have already told you why. When you access a static/Shared field of a class you are accessing the one and only value of that field for the class. It doesn't matter where you access it when you access it. It's the same class and it's the same field so it's the same value. For the third time, if you expect a different value of the gVariables.g_ZoomFactor field every time you create a new instance of your MyUC type then you need to create a new instance of gVariables and use an instance field, which is the exact opposite of a static/Shared field. Again, you're complaining that a static/Shared field is working exactly the way static/Shared fields are supposed to work. If you don't want a field that works that way then stop using a static/Shared field. I'm not going to waste any further time repeating myself.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: variables in a sealed class retain value after form close

    You are thinking that a Shared variable is like any other variable in a class in that it exists with an instance of a class. That's not the case. A Shared variable exists above all instances of the class. It exists before an instance of the class is created, and continues to exist after all instances have been destroyed. You could even think of it as not being part of the class at all, aside from the fact that you need to reference it through the class name.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: variables in a sealed class retain value after form close

    Quote Originally Posted by PlausiblyDamp View Post
    It is a general info, not mention my particular circumstance. frmDemo1 already closed, why the gVariables class is still alive, that is the point i don't understand.

  12. #12
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: variables in a sealed class retain value after form close

    Quote Originally Posted by DaveDavis View Post
    It is a general info, not mention my particular circumstance. frmDemo1 already closed, why the gVariables class is still alive, that is the point i don't understand.
    Because static variables belong to the class, not an instance of the class. The lifetime is not the same as the lifetime of an instance.
    The static variable retains its value for the lifetime of the application, it has nothing to do with the lifetime of any instance of the class.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: variables in a sealed class retain value after form close

    Quote Originally Posted by Shaggy Hiker View Post
    You are thinking that a Shared variable is like any other variable in a class in that it exists with an instance of a class. That's not the case. A Shared variable exists above all instances of the class. It exists before an instance of the class is created, and continues to exist after all instances have been destroyed. You could even think of it as not being part of the class at all, aside from the fact that you need to reference it through the class name.
    I know you are correct, but it is just beyond my knowledge (my common sense on programing). I am not at your level. I hope you can tolerant my stubborn.

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: variables in a sealed class retain value after form close

    Quote Originally Posted by PlausiblyDamp View Post
    Because static variables belong to the class, not an instance of the class. The lifetime is not the same as the lifetime of an instance.
    The static variable retains its value for the lifetime of the application, it has nothing to do with the lifetime of any instance of the class.
    I caught your last sentence.
    it confused me before, now I have better understand.

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: variables in a sealed class retain value after form close

    Quote Originally Posted by PlausiblyDamp View Post
    Because static variables belong to the class, not an instance of the class. The lifetime is not the same as the lifetime of an instance.
    The static variable retains its value for the lifetime of the application, it has nothing to do with the lifetime of any instance of the class.
    I like your describes that I can understand. Now I 'reset' those 'global' static variables when I create new instance of my UserControl:

    Code:
    public MyUC()
    {
         //...
         gVariables.g_ZoomFactor = 1.0f;
         //...
    }
    Appreciated on experts' education, I learn a new (for me) thing. I have to investigate my other projects to correct potential issues on static variables.

  16. #16
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: variables in a sealed class retain value after form close

    [QUOTE=DaveDavis;5579686]I like your describes that I can understand. Now I 'reset' those 'global' static variables when I create new instance of my UserControl:

    Code:
    public MyUC()
    {
         //...
         gVariables.g_ZoomFactor = 1.0f;
         //...
    }
    Why are you using a static in this case? Is the ZoomFactor common to all user controls? i.e. should changing the zoom factor on one control change it on all controls? If not then why are you tracking ZoomFactor as a global variable?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width