开发者

public bool var not updating its value

I've created a public bool LogedIn; in my login.cs:

if(login successful condition)
LogedIn = true;
else
LogedIn = false

But when I access this var from another form with Login Log = new Login();

by using if(Log.LogedIn) the LogedIn variable is always false, even after successful login by the user.

Why this is not working/updating its value outside its parent form?

Updating the code: Login.cs

public bool isLogedIn;
private void button1_Click(object sender, EventArgs e)
{
        if (i>-1 && (textBox2.Text == DS.Tables[0].Rows[--i][0].ToString()))
        {
            this.DialogResult = DialogResult.OK;
            isLogedIn = true;
        }
        else
        {
            MessageBox.Show("Invalid password supplied for username \"" + comboBox1.Text + "\"", "Login Error.....", MessageBoxButtons.OK);
            isLogedIn = fa开发者_运维问答lse;
            return;
        }
}

Checking for the updated value in Home.cs

        private void Home_Load(object sender, EventArgs e)
        {
            if (Log.isLogedIn)  // Always False at this position.
            {
                label18.ForeColor = System.Drawing.Color.Green;
                submitButton.Enabled = true;
            }
            else
            {
                label18.ForeColor = System.Drawing.Color.Red;
                submitButton.Enabled = false;
            }
}

I've checked again... I'm not having double instance of this variable in Login.cs form.

Here's how I'm calling Login.cs form via Home.cs (main form). Hope this helps...

private void loginToolStripMenuItem_Click(object sender, EventArgs e)
{
    Log.FormClosed += new FormClosedEventHandler(Log_FormClosed);
    Log.ShowDialog(this);
    Log.BringToFront();
}

void Log_FormClosed(object sender, FormClosedEventArgs e)
{
    if (Log.isLogedIn)
    {
        // Something here
    }
    else
    {
        // Something here
        if (Log.DialogResult == DialogResult.Cancel)
            Log.Hide();
    }
}


I assume you have a form called Login in your application. Ignore the rest if assumption is wrong.

You are not referring to the correct instance of the Login form. In windows application, there is a collection called Application.OpenForms. This contains all the open form instances in your application. To access the correct Login form, try this:

Application.OpenForms.OfType<Form>().Where(x => x is Login).FirstOrDefault()

Make sure you have Login form always open to perform this task. You can make use of Hide instead of Close or CloseDialog for the Login form.

If you are closing the Login form, you can create static class which is accessible from each of the forms keep the properties there.


It seems that you have more than one instance of the Login class, each one with its isLogedIn var.
It is not clear where you are instantiating Login with your Login Log = new Login(); line. Have you tried to put a breakpoint there and see how many times it gets hit?
Another thing you could do is put a breakpoint on the line where isLogedIn is set, and another one where you read it. When the setting breakpoint is hit add a watch to the instance of Login (in this case add a watch to this) and choose Make Object ID from the right click menu. Your instance will be marjked by a "#1" Then do the same for the variable Log when the reading breakpoint is hit. If the mark is different (i.e. "#2") you can be sure that you are reading something different from the variable you set before.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜