Search code examples
javavariablesprivategosuguidewire

Can I add more than one field to a single attribute


I was wondering if it's possible to add 2 different attributes to a single attribute. For example I have two attributes:

private var modelYear:int as ModelYear
private var manufacturedCd:String as ManufacturerCd

I would like to put both of these attributes into one attribute called "personalVehicle". So then I can derive these values from personalVehicle on another class.

To clarify my question:

  1. Can I add two attributes into one attribute called personalVehicle?
  2. If so, how can I pull just the modelYear from the personalVehicle attribute on another class?

Thanks


Solution

  • I'm assuming here that you are using Gosu in one of the Guidewire's apps (hence the guidewire tag).

    In these apps you have access to gw.util.Pair class which is basically a container for two values.

    var personalVehicle = new Pair<Integer, String>(2010, "something")
    

    You can access stored values through First and Second properties.

    print(personalVehicle.First) //prints 2010
    print(personalVehicle.Second) //prints "something"
    

    Please note that First and Second tell you nothing about what is stored inside these properties. In your case (especially if you want to pass your personalVehicle to other classess) it may be better to define separate class storing modelYear and ManufacturerCd.

    class PersonalVehicle {
        private var modelYear : int as ModelYear
        private var manufacturedCd : String as ManufacturerCd
    }
    

    Using such class would make your code more readable.