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 ?

  • Did you initialise mcontroller e.g. mcontroller = new MainController(); ? – Davin Casey Jun 22 '15 at 10:17
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;
  }
}
  • I tried your second method but I get an error saying "Constructor not defined"! – Tinkerbell Jun 22 '15 at 10:27
  • 1
    you should define a constructor to ur controller for that to work – Vamsi Krishna Gosu Jun 22 '15 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.