You need to sign in to do that
Don't have an account?
adrissel
Can't access variable in method
My question is of a specific and general nature. First the specific aspect. I have the following code in my controller:
Ignoring other aspects of the code that may be missing (I didn't paste the entire controller) why in the world is it giving me a Compile Error for both the "setRootCat" and "prefix" variables saying that they "don't exist"? Both variables are declared public. Shouldn't I be able to access them later in the controller?
The general aspect of my question is why Apex doesn't allow simple usage of a variable across various functions in a class. PHP, Java, Javascript...they all allow this, but declaring and using variables in various Apex methods in a class seems very cumbersome. Maybe I'm missing something simple?
Thanks ahead of time!
public class ListVisableCategories { // INSTANTIATE CONTROLLER public ListVisableCategories(){} // SET VARIABLES public Set<String> setRootCat{get;set;} public String prefix = Site.getPathPrefix(); // DESCRIBE VISABLE CATEGORIES public static List<String> describeVisibleCategories(Map<String, String> mLabelCat, Map<String, List<String>> mCatStructure, Map<String, String> mParentCategory){ if (prefix == '/xyz') { Set<String> setRootCat = new Set<String>{'Buyer_Articles'}; } else if (prefix == '/abc') { Set<String> setRootCat = new Set<String>{'All'}; } List<String> listVisableCategories = new List<String>(); ........ // IF APPLICABLE, GATHER ALL CHILD CATEGORIES if(setRootCat.contains(dc.getName())){ for(DataCategory dcChild : dc.getChildCategories()){ listVisableCategories.add(dcChild.getName()); } } else{ listVisableCategories.add(dc.getName()); } } return listVisableCategories; } }
Ignoring other aspects of the code that may be missing (I didn't paste the entire controller) why in the world is it giving me a Compile Error for both the "setRootCat" and "prefix" variables saying that they "don't exist"? Both variables are declared public. Shouldn't I be able to access them later in the controller?
The general aspect of my question is why Apex doesn't allow simple usage of a variable across various functions in a class. PHP, Java, Javascript...they all allow this, but declaring and using variables in various Apex methods in a class seems very cumbersome. Maybe I'm missing something simple?
Thanks ahead of time!
Your method seems to be a static method.
In apex you can not access non static variables inside static methods.
Thanks,
N.J
All Answers
Your method seems to be a static method.
In apex you can not access non static variables inside static methods.
Thanks,
N.J