0

Hi I a trying to set a Boolean value in a wrapper based on a condition.

Wrapper Class

 public class Wrapper{
  public String pf {get; set;}
  public Boolean show {get; set;}

   public Wrapper (String pf, Boolean show){
      this.pf = pf;
      this.show = true;
   }
 }

Controller

  List<Wrapper> epf= new List<Wrapper>();

  for(String s : << A set of String >>){ 
       Boolean show;
       if( << condition >> ){
         System.debug('IT CONTAINS  '+s);
         show = true;
       }else{
         System.debug('IT DOES NOT CONTAINS  '+s);
         show = false;
       }
       epf.add(new entryPortfolioWrapper(s, show));
    }

  System.debug('CHECK>>'+epf);

In the debug log 'CHECK>>', all the added wrapper objects has 'show' values set 'true' even though there are cases that should be false. Somehow it only takes the default value set in the wrapper class. What am I missing here ?

1
  • remove Boolean show; in for loop. add it above/ outside for loop. post your complete code so that i can give the answer Commented Jan 24, 2019 at 4:54

2 Answers 2

0

In your wrapper, you're not setting a default value, you're actually setting the value. (assuming Wrapper is the class you're instantiating with new entryPortfolioWrapper())

Change it to:

public Wrapper (String pf, Boolean show){
  this.pf = pf;
  this.show = show; // actually use the incoming value.
}
0

In your wrapper's constructor, you are always setting boolean "show" as true. Instead set the boolean to a value which is passed to the constructor as a parameter.

This is how your constructor should look like

public Wrapper (String pf, Boolean show)
{ 
    this.pf = pf; 
    this.show = show; //true; 
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .