0

Hi i have code that shows an error "Error: simplegetset Compile Error: Variable is not visible: userinput at line 14 column 15".I just want to know is this not right way to define get and set here is the code.

public class simplegetset
{
  // public String userinput{get; set;}
   public string userinput{set;}
   //public string userinput{get;}
  /* public void setUserinput(string userinput)
   {
       this.userinput = userinput;
   } */


  public void getUserinput()
   {
       return userinput;
   } 
}

And same if I declare getter setter like this and when I write like this

public class simplegetset {  
    public string userinput{get;}                                                                                                                                                 public string userinput{get;}
    public void setUserinput(string userinput){
       this.userinput = userinput;
    } 
} 

Its shown an same error "Variable is not visible" is this not right way to declare getter and setter?

  • They're both right technically, but as Caspar pointed out you have a syntax error in your getter method (void return on a getter doesn't make sense). I avoid mixing the two types though. There is also a longer form of the get; set; that allows for processing (e.g.: get { // code here and return statement }) – drakored Sep 22 '16 at 7:44
  • Take a look at the Apex Properties documentation for examples of the get/set syntax that is shorthand for writing getter and setter methods. – Keith C Sep 22 '16 at 7:52
  • I added another example below. Like Keith C said, read the docs - it's pretty clear. – Caspar Harmer Sep 22 '16 at 9:35
  • Actually, the problem you are really having is that you are MIXING the two types of getters/setters - either use the {get;set;} syntax or use the getVarName syntax - not a bit of both. – Caspar Harmer Sep 22 '16 at 9:37
  • Its mean its not good way to declare getter setter either you declare in single line like this "public String userinput{get; set;}" or by defining seprate both getter and setter "public void setUserinput(string userinput){ }" and like "public String getUserinput()" this pattern – Bonny Sep 22 '16 at 9:42
1

Redefine your getter like this:

public String getUserinput()
{
   return userinput;
} 

You are not returning a string from the getter, currently you are returning void, which won't work for a getter.

Another example:

  public String userinput {
  get {
      return this.userinput;
  }
  set {
      this.userinput = value;
  }

Note, value is predefined and you need to use this name.

  • Hi casper i redefine using string return type but its shows same error Variable is not visible: userinput at line 14 column 15 – Bonny Sep 22 '16 at 9:19
  • hi casper what if i want to pass value from vf page beacuse i used vf page for this controller – Bonny Sep 22 '16 at 9:47
  • The page and controller will handle it for you. All you really need is this: public String userinput {get;set;} - this is because you are not doing anything else in the getters/setters and can use the default. – Caspar Harmer Sep 22 '16 at 9:52
  • Thanks Casper i firstly tried same stament but i learn to use getter and setter in different ways and now i know there is only two right way. first with single statement as you define above and second right getter and setter seprately Thanks. – Bonny Sep 22 '16 at 9:58
  • Oh so you already knew how to use! I see know. Yep, I think there are only two ways :) – Caspar Harmer Sep 22 '16 at 10:01

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.