user10698708
user10698708

Reputation:

I can't set a value to public variable

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

Answers (2)

Brian Walker
Brian Walker

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:

  1. Make the variable static: public static bool JoulVarQuestionMsgStatus;
  2. When you call it use an instance of the Cls_Options class that is created and stored somewhere.

Upvotes: 0

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

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

Related Questions