1

I got a boolean variable in my controller class and I want to access that variable to check a condition in another helper class.

Ex: Class MainController {
       public boolean showValue {get; set;} // globally declared
}

I created an instance of the MainController class in my helper class and tried accessing the boolean value but I get an error saying

"Variable does not exist"

, following is what I tried in the helperClass

Ex: Class HelperClassForController {
      public MainController mcontroller {get; set;}

      public static method1(){
       if(mcontroller.showValue){ // I get the error here
         // Some Code
       }
         return value;
      }
    }

How can I access the boolean variable ?

1
  • Did you initialise mcontroller e.g. mcontroller = new MainController(); ? Commented Jun 22, 2015 at 10:17

1 Answer 1

3

The error is not with the boolean variable of the controller.. its with the instance variable you have created for the controller.

you are trying to access an instance variable from a static method

if you want to keep your method1 as static, then you have to make the controller variable also as static or you have to move the controller variable inside the method.

Class HelperClassForController {
  public static MainController mcontroller {get; set;}

  public static method1(){
   if(HelperClassForController.mcontroller.showValue){ // I get the error here
     // Some Code
   }
     return value;
  }
}

OR

Class HelperClassForController {

  public static method1(){
    MainController mcontroller = new MainController(); 
   if(mcontroller.showValue){ // I get the error here
     // Some Code
   }
     return value;
  }
}
2
  • I tried your second method but I get an error saying "Constructor not defined"! Commented Jun 22, 2015 at 10:27
  • 1
    you should define a constructor to ur controller for that to work Commented Jun 22, 2015 at 10:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.