-1

Here is the situation:

I am passing a value from my component to its controller

 <apex:component controller="MyController">
    <apex:attribute name="someObj" assignTo="{!receivedObject}" type="Sobject"/>
     .....some other code
  </apex:component>

At the controller side: MyController

  public with sharing class MyController {
     public Sobject receivedObject{ get; set; }

     public MyController() {
         //code where I am using the receivedObject for doing some initialization
     }
  }

The problem is it shows the receivedObject is null, when the constructor is being executed. The value of receivedObject is set after the constructor has finished with its execution.

Is there any way I can get its value in the constructor itself. Overriding the get and set of the receivedObject is not an option (dont ask why).

Help is appreciate. Thanks.

1
  • @Neeraj - did you even read the answers you got in your earlier question? The top voted answer mentions this behavior in the second sentence.
    – jkraybill
    Apr 2, 2013 at 2:21

2 Answers 2

1

You can't set the value of an object member variable before the constructor is called because no instance of the object has been constructed at that point in time.

Components can prove a bit challenging in this regard, but one way around it is to write getters for all of your public member variables that are used on the page. In each, check to see if the instance has been initialised or not, and if not perform the initialisation (use a private boolean field and an initialisation method).

0

Unfortunately not. This is standard behaviour in OO programming languages. You cannot set the state of an instance of a class (via methods, members or properties) before it has been constructed. When Visualforce constructors your components controller it is simply following this behaviour.

There is not really a 'action' attribute for a apex:component like apex:page for example. So you don't have a place to the type logic I imagine your needing. Implementing such logic in the set methods is also problematic since the order these are called is not defined. The only option you have is to implement it in the get method/s used by a binding in your component.

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