Reputation:
Cls_Options
.Inside this class I have the next variable:
public bool JoulVarQuestionMsgStatus;
I have (A) form contains a button.
When clicking on this button I set a value for the variable JoulVarQuestionMsgStatus
.
private void btnOK_Click(object sender, EventArgs e)
{
Cls_Options.JoulVarQuestionMsgStatus = true;
Close();
}
I show form (A) through form (B) and:
I have an if statement to see what's the value of JoulVarQuestionMsgStatus
, If it was true
then doing something and if false
doing something else.
MSGs.FrmMsgQuestion FrmMsgQuestion = new MSGs.FrmMsgQuestion();
FrmMsgQuestion.lblMsg1.Text = Cls_Options.MsgSaveNew;
FrmMsgQuestion.ShowDialog();
if (Cls_Options.JoulVarQuestionMsgStatus== true)
{
int NewID = Cls_Countries.Joul_GetIDs();
this.txt1.Text = NewID.ToString();
this.txt2.Text = null;
this.txt2.Select();
}
my problem is: when I test the code I see that the variable in the next code saves the value that I assigned:
private void btnOK_Click(object sender, EventArgs e)
{
Cls_Options.JoulVarQuestionMsgStatus = true;
Close();
}
but when I see value of the variable in if statement, I see that the variable has no value , it's keep its default value!!! Help me, please
Upvotes: 0
Views: 124
Reputation: 8908
public bool JoulVarQuestionMsgStatus;
defines an instance variable. There is a separate copy of this variable for each instance of the Cls_Options class.
You are trying to use this variable as a static variable. In this case there would be one copy of this variable (tied to the class not to an instance of the class).
To fix this you would do one of 2 things:
public static bool JoulVarQuestionMsgStatus;
Upvotes: 0
Reputation: 34152
1.You can set Cls_Options
class property, if it is a public static class, that in your case it seems not to be the case.
public static class Cls_Options
{
public static bool JoulVarQuestionMsgStatus = false;
}
then you can set the its value like:
Cls_Options.JoulVarQuestionMsgStatus = true;
2.If Cls_Options
is not static, then it must be a public class, and then you may set properties of an instance of the class like:
Cls_Options cls_o = new Cls_Options();
cls_o.JoulVarQuestionMsgStatus = true;
Upvotes: 1