简体   繁体   中英

How to access constructor variables into the methods with in the same class in c#

Here is my code.i am not able to access constuctor value in the function.

 public partial class ForgotPassword : UserControl
    {
         private string mobile_num2 = "";
         public ForgotPassword(string _mobile_num)
         {

             mobile_num2 =_mobile_num;
             MessageBox.Show(mobile_num2);//Getting Value here
             InitializeComponent();

         }

       private void btn_submit_Click(object sender, RoutedEventArgs e)
       {
            string val_conf_pwd;
            string conf_pwd = txt_new_conf_pwd.Password;
            val_conf_pwd = c.validate_conf_Password(pwd, conf_pwd);

            if (val_conf_pwd == "success")
            {
               MessageBox.Show(mobile_num2);//Getting Null Value Here
            }
        }
    }

If this is your actual code (and it's not just that you actually declared another local variable within your constructor instead of assigning the value to the field) then I suspect the problem is that the instance that btn_submit_Click is being invoked on isn't the same one as the one you're constructing with the non-null value.

So you need to look at where you're constructing any instances of ForgotPassword - you won't be able to just construct a new instance and hope that it will have the same mobile_num2 value as an existing instance, unless you explicitly pass the right value to the constructor.

(As an aside, I'd encourage you to use more conventional names for methods and variables - just camelCase without underscores. But obviously that's not part of what's going wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM