14

I'm trying to build a VF component that references an attribute that is an instance of an inner class. I followed the suggestion here on the Apex boards but it does not compile for me using V28.0. (Note the boards post was about lists of inner class objects - I only need a single instance)

Interface, Apex class, VF component follow:

public interface VFComponentAttributable {
// To make inner classes available as VF component attributes
// http://boards.developerforce.com/t5/Visualforce-Development/Error-using-inner-class-in-component-attribute/td-p/147856
}

public with sharing class Foo {
   public class InnerBar implements VFComponentAttributable{
      public String  myVbl {get; set;}
  }
}    


<apex:component >
<!-- Generates compiler error 'Unknown property VFComponentAttributable.myVbl' -->
<apex:attribute name="bar"                      description="instance of inner class"
                type="VFComponentAttributable"          required="true"/>

<apex:outputText value="{!bar.myVbl}"/>  


<!-- This doesnt work either, generates compiler error  'Apex class InnerBar does not exist
<apex:attribute name="bar"                      description="instance of inner class"
                type="Foo.InnerBar" required="true"/>
<apex:outputText value="{!bar.myVbl}"/>             -->

I know I can refactor and make the inner class an outer class and it will work but I'd rather not if I can avoid it.

3 Answers 3

11

I've run across this as well and also resorted to refactoring my inner-class to the top level. The documentation isn't clear on this, but considering that the type attribute is considered limited, I think it's fair to assume that only top-level classes are supported. From the Visualforce Developer's Guide (pg. 285):

Only the following data types are allowed as values for the type attribute:

  • Primitives, such as String, Integer, or Boolean.
  • sObjects, such as Account, My_Custom_Object__c, or the generic sObject type.
  • One-dimensional lists, specified using array-notation, such as String[], or Contact[].
  • Maps, specified using type="map". You don't need to specify the map's specific data type.
  • Custom Apex types (classes).
1
  • Thanks @Adam; I was forlornly hoping for a miracle here from one of the gang.
    – cropredy
    Commented Aug 27, 2013 at 22:23
3

The problem here is that your Component is receiving an instance of VFComponentAttributable which does not have the myVbl field.

If you change your VFComponentAttributable and Foo definitions to the following it should work.

public interface VFComponentAttributable 
{
    // To make inner classes available as VF component attributes
    // http://boards.developerforce.com/t5/Visualforce-Development/Error-using-inner-class-in-component-attribute/td-p/147856
    String getMyVbl();
    void setMyVbl(String value);
}

public with sharing class Foo 
{
    public class InnerBar implements VFComponentAttributable
    {
        private String  myVbl;

        public String getMyVbl()
        {
            return myVbl;
        }

        public void setMyVbl(String value)
        {
            myVbl = value;
        }
    }
}  
1
  • Alex -- I know you answered this long ago and I should have responded as your answer is correct and works fine (having just tried it). I've been stewing over this for some time but finally had a chance to revisit this on a new project.
    – cropredy
    Commented Jan 19, 2016 at 17:49
0

Why not in Foo:

Public foo{

    Public class innerBar{
    //etc
    }

    Public innerBar myBar {get;set;}

}
1
  • Yes, this would work in some use cases but my component is being invoked within a apex:repeat that is traversing a list of InnerBar so an instance of foo doesn't know which instance of InnerBar is being passed to the component.
    – cropredy
    Commented Aug 27, 2013 at 22:21

You must log in to answer this question.

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