CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2004
    Posts
    218

    Help with accessing a globe variable on a form from other form problem, thank you

    Hi,
    I am sorry for poor programming skill.
    I have a form. Inside, I declared a global variable loginflag, the initial value is false, when users login, the value becomes true.But when I accessed this variable in other form, it always shows false.
    Code:
    //declare 
    public bool loginflag=false;
    Code:
    //update the loginflag to true
     private void btOK_Click(object sender, EventArgs e)
            {
                if (txtusers.Text == "")
                   .......
                        
                else
                 {
                          
                LoginFlag = true;
                cSingleton.Instance.Main.Show();
                     
                 } 
            }
    Code:
    //create singleton for accessing between forms 
      public sealed class cSingleton
        {
            FMain _Main;
            FLogin _login;
    
    
            public cSingleton() 
            {
             _Main = new FMain();
          
            _login = new FLogin();
    
            }
            static readonly cSingleton _Instance  = new cSingleton();
           
            public static cSingleton Instance
            {
                get { return _Instance; }
            }
            public FMain Main
            {
                get { return _Main; }
            }
       
    
            public FLogin Login
            {
                get { return _login; }
            }
     
        }
    Code:
    //access loginflag from other form
                  else
                    {
                        while (OpenComPort())
                        {
                            if (dr == DialogResult.Cancel)
                            {
                                dr = DialogResult.Retry;
                                
                                return;
                         }
                           
                            MessageBox.Show(cSingleton.Instance.Login.LoginFlag .ToString());//always false. problem here
    }
    What causes this? Thank you.

  2. #2
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Help with accessing a globe variable on a form from other form problem, thank you

    I presume that the first two code blocks are from FLogin class?
    Show us the code for the LoginFlag property, and make sure to denote the containing class if you're posting code from multiple classes. Also, I'd like to see what is passed to Application.Run() method in Program.cs.
    BTW, the constructor of your singleton mediator class should be private, otherwise, you'll be able to construct more than one "singleton".

    The best I can think of without seeing the declaration of LoginFlag and the call to Application.Run(), is that either there's something wrong with the LoginFlag property, or that the FLogin object where you set the login flag is not the same as the FLogin object held inside the singleton instance (meaning that you have a duplicate copy of your login form, which is never shown).

  3. #3
    Join Date
    Feb 2004
    Posts
    218

    Re: Help with accessing a globe variable on a form from other form problem, thank you

    Thank you very much for replying.
    Yes, the first two codes are from Flogin.cs
    The LoginFlag property and Application run as below for Flogin.cs.
    Code:
        public FLogin()
            {
                InitializeComponent();
            }
    
            public bool LoginFlag
            {
                get { return loginflag; }
                set { loginflag = value; }
            }
    
            [STAThread]
            static void Main()
            {
                Application.Run(new FLogin());
             }
    Sorry, if you need further information, please let me know.
    Thank you again.

  4. #4
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Help with accessing a globe variable on a form from other form problem, thank you

    Yeah, the Form passed to Application.Run() and the one in the cSingleton are two separate instances.

    Just replace the line
    Application.Run(new FLogin());
    with:
    Application.Run(cSingleton.Instance.Login);

  5. #5
    Join Date
    Feb 2004
    Posts
    218

    Re: Help with accessing a globe variable on a form from other form problem, thank you

    Thank you very much
    TheGreatCthulhu
    I can get the correct value now.
    Thank you again.

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

Featured