2

I know this type of notation

public class BasicProperty {
   public integer prop {
      get { return prop; }
      set { prop = value; }
   }
}

BasicProperty bp = new BasicProperty();
bp.prop = 5;                   // Calls set accessor
System.assert(bp.prop == 5);   // Calls get accessor

The problem is that I want to use this to set a map, but I dont know how to pass two parameters to setter property.

public Map<String,Boolean> var1 {get;
                            set{
                               var1.put(key,value);
                            }
                          }

How can I pass the key and de value to setter method?

2

When you set the value for var1, you are setting the value for the whole variable. That means that you have to set it as something of the type map<String,Boolean>. If you want to add a value to the map, you could get it first, and then put a value into it.

public class testMap{

public Map<String,Boolean> var1 {get; set{var1 = value;}}

    public testMap(){
        system.debug(var1);  // NULL
        var1 = new Map<String,Boolean>();
        system.debug(var1);  // a map with no elements
        var1.put('true',true);
        system.debug(var1);  // a map from 'true' to true
    }

}

You can test this out by running the following as anonymous apex:

new testMap();

The first debug statement will be null since the variable has been declared, but does not have a value set to it yet.

The second debug statement will be an empty map, since we created a new map with an empty constructor and set it as the value of var1.

The third debug statement will show a value since we put a key value pair into the map.


Then if you change the setter to something else,

public Map<String,Boolean> var1 {get; set{var1 = new map<String,Boolean>{'false'=>false};}}

It should give you a better idea of how the setter method works. It is essentially an override on a var1 = variable assignment. You might have expected the second debug statement to still be an empty map, but the map that was created in the testMap class constructor was ignored and instead var1 was set to be a different new map in the setter.

| improve this answer | |
1

Try like that:-

public class BasicProperty {
   public Map<String,Boolean> prop {
      get { 
            if(prop == null)
                 return new Map<String,Boolean>();
            return prop; 
          }
      set { prop = value; }
   }
}

BasicProperty bp = new BasicProperty();
bp.prop = bp.prop.put('key',true);        // Calls get and then set accessor over it. Reference used.
System.assert(bp.prop.get('key') == true);   // Calls get accessor

Maps are collection type object you have to get their reference first then add value into it.

or you can do it like:

BasicProperty bp = new BasicProperty();
bp.prop = myPredefinedMap       // Calls  set accessor over it
System.assert(bp.prop.get('key') == true);   // Calls get accessor
| improve this answer | |
1
public class basicProperty{
   // property
   public Map<String, Boolean> prop {get; set;}
   // constructor
   public basicProperty(){
       basicProperty bp = new basicProperty();
       bp.prop = bp.prop.put('key', true);
       }
}

The key here is that the constructor is called before the getter, and so the code executes before the Visualforce page communicates with the controller. This design pattern of utilising properties and setting their initial state in the constructor uses a lot less code (Learning Apex Programming, Matt Kaufman).

| improve this answer | |

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.