+ Start a Discussion

Assigning different types of objects to a variable [Wrapper class question]



I am hoping that this is an easy question.


Some time ago I wrote a wrapper class for Opportunity Line Items. It does a heap of useful stuff for me and is used in multiple extensions (and, as such, VF pages).


Anyway, we are about start using Quotes. Quote Line Items and Opportunity Line Items are almost identical and most of the functions in the wrapper class I would like to reuse.


public class OpportunityLineItem_VF_Wrapper {

    // Getters & Setters
    public OpportunityLineItem oli {get;set;} //the OLI being edited/extended.
    public String uniqueID{get; set;}
    public String quantityText{get; set;}
    // a heap of other variables

    // Constructor
    public OpportunityLineItem_VF_Wrapper(OpportunityLineItem theOLI) {
oli = theOLI; //etc, etc


I could just copy the whole thing and maintain two sets of code but it seems to me that it would be better to have two constructors, one for OLIs and one for QLIs. I am not sure in Apex how I define the variable though (for the moment let's assume the name stays as "oli"):


  1. Can I define a public variable with class-wide scope in a Constructor?
  2. Can I define 'oli' as as some sort of base class?
  3. Can I subvert a 'qli' object as an 'oli' one? [ oli = (oli) theOLI; ]

Or any other workable way?


Would appreciate any pointers of things to consider.