开发者

Simple question regarding reference variables and passing of variables in C#

I am creating a WindowForm application and right now i have a small problem. I have a register form which will ask user for username, mobile number and password. Upon registering the the information will be written to a text file. And the user will be shown a login form once the user login successfully i will create a user object from a user entity class and pass it to my main form and display the main form hiding the login form. After certain period of inactivity the main form will be hidden and the login form will be shown to the user.

public class User
{
    private string userName;
    private string mobileNumber;


    public User(string uName, string mnumber) 
    {
         userName = uName;
    mobileNumber = mnumber;
    }

    public string MobileNumber
    {
        get
        {
            return mobileNumber;
        }
        set
        {
            mobileNumber = value;
        }
    }

    public string UserName
    {
        get
        {
            return userName;
        }
        set
        {
            userName = value;
        }
    }

}

public partial class Login : Form
{

     //Other codes
     //Upon successful login
     User user = new user(username,mobilenumber);
     //Hide the Login Form
     this.Hide();
     //Display the main Form
     MainForm main = new Ma开发者_运维技巧inForm(ref user, this);
     main.Show();
}

public partial class MainForm : Form
{  
     private User user;
     private Login login;

     Public MainForm(ref User user, Login login)
     {
          this.user = user;
          this.login = login;
     }
 }

The form will toggle often login -> MainForm -> login. Here is my question. , when i pass over the user object as reference variable to MainForm as shown above any change made to the user object in the MainForm class will be reflected as well in the login class but the user object in the MainForm will not have the latest value and it will be the value when the mainForm object is first created. Why is that so why is the changes made in MainForm being reflected in the login form but not in the MainForm class ? Thanks in advance for any help provided. Cheers.


You can't do this. There is no such thing as a reference field or reference variable. Just have an event that fires when the user changes, or let calling code poll for a change in the form's user variable. There's no other way to do it.


Using the ref keyword makes no difference here. If it is the same object any changes to it will be reflected everywhere.


First of all, you're User object is not a ValueType therefore the ref modifier is not necessary in this particular case. You would only need to do this when you are using Pass By Value semantics, were User defined as a struct.

Second, I'm not sure you are handling you'r problem the right way. You should think about refactoring your code to work similar to:

  1. Application starts with MainForm:
  2. Uppon Form Load (this happens the first time the Main Form is about to render) create User Login and show it as a dialog (you will want to do this in a seperate method so that you can reuse it in step 4). Make an option available on this Login Form to Register a new user.
  3. Upon succesful login, store the User only in Main Form and close the Log In form (do not hide it, Close and Dispose and just create a new one when you need it). Any subsequent edits to the User should map to this variable.
  4. If inactivity period expires (via an event handler), cancel the current user and show the User login again and redo step 2.

Hope this helps and you get the general idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜