function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jancy MaryJancy Mary 

How to access non getter setter variables from Controller on VF Page?

Hello All,

Need some information, suppose I have 2 fields called Age & DOB in a controller, these are not a getter setter fields, I have a button on the VF page, when I click the button these fields should get loaded to the page, how to achieve this.

Thanks in advance.
Jancy
Best Answer chosen by Jancy Mary
Vasani ParthVasani Parth
+1 . You need to basically understand the getter and setter methods well.

Visualforce requires a "getter" and "setter" to reference a variable in the controller or extension. Without a getter or setter, even public or global variables cannot be referenced in Visualforce expressions.

"get;" is basically: public *datatype* getVarName() { return varName; }
"set;" is basically: public void setVarName(*datatype* value) { varName = value; }

There are subtle differences though. For example, the function doesn't actually exist; it's called implicitly. You can make variables read-only by not including set;, and you can make them write-only by not including get;.Inside Apex Code, you can also use these to control readability and writeability.

For example:
public static Integer counter {
    get { if(counter == null) counter = 0; counter++; return counter; }
    private set; }
This counter increases by 1 each time it's used, but can't be set outside of the class that contains it.
public class OtherClass {
   public void someMethod() {
       System.debug(FirstClass.counter);
       System.debug(FirstClass.counter);
       FirstClass.counter = 10; // Compiler error
   }
}
Detailed information : http://www.forcetree.com/2009/07/getter-and-setter-methods-what-are-they.html

Please mark this as the best answer if this helps
 

All Answers

bob_buzzardbob_buzzard
You'll have to make that information available via something that does have a public getter, otherwise you just can't display it in Visualforce. If you aren't using the automatic property syntax then you can create a public getter method, so rather than having:
 
public String myStr {get; set;}

You could have
 
private String myStr;

public String getMyStr()
{
    return myStr;
}
Jancy MaryJancy Mary
Hi Bob,

Thanks so much for helping me on this, so this is what I understood by your reply, to access the string from a controller either it has to be made public getter or the string has to be return through some public getter method.And there are no other ways to achieve this apart from these two what you mentioned, is my understanding correct?

Thanks again,
Jancy
Vasani ParthVasani Parth
+1 . You need to basically understand the getter and setter methods well.

Visualforce requires a "getter" and "setter" to reference a variable in the controller or extension. Without a getter or setter, even public or global variables cannot be referenced in Visualforce expressions.

"get;" is basically: public *datatype* getVarName() { return varName; }
"set;" is basically: public void setVarName(*datatype* value) { varName = value; }

There are subtle differences though. For example, the function doesn't actually exist; it's called implicitly. You can make variables read-only by not including set;, and you can make them write-only by not including get;.Inside Apex Code, you can also use these to control readability and writeability.

For example:
public static Integer counter {
    get { if(counter == null) counter = 0; counter++; return counter; }
    private set; }
This counter increases by 1 each time it's used, but can't be set outside of the class that contains it.
public class OtherClass {
   public void someMethod() {
       System.debug(FirstClass.counter);
       System.debug(FirstClass.counter);
       FirstClass.counter = 10; // Compiler error
   }
}
Detailed information : http://www.forcetree.com/2009/07/getter-and-setter-methods-what-are-they.html

Please mark this as the best answer if this helps
 
This was selected as the best answer
Jancy MaryJancy Mary
Hey Vasani,

That's a nice blog with simple explanation, well I could sense what you mentioned about referencing a variable on a VF page from the controller or extension should be of “getter” and “setter”. I got more clarity on it going through the blog you shared, and thanks for your explanation too.

Cheeeeerrrrs!!!
Jancy