0

I'm using ColdFusion, but I'd be interested to know how other languages cope with this.

Is there a better way of returning all of an objects variables (getters) without writing a massive toString() method on the object.

variables.oCity = createObject("component", "_lbr._core._locations.city").init();

variables.oCity.setName(request.parameters.sCityName);
variables.oCity.setCountryID(request.parameters.nLocationCountryID);

if(request.parameters.nStateID eq 0){
    variables.stArgs = {};
    variables.stArgs.sState = request.parameters.sLocationCountry;
    variables.stArgs.nCheckCountryID = request.parameters.nCountryID;
    variables.oCity.setStateID = application.stObj.oLocationBusiness.getState(argumentCollection=variables.stArgs).getStateID();
} else {
    variables.oCity.setStateID = request.parameters.nStateID;
}

My code looks like that. What I'd like is to output everything I have just set (as well as anything that the object defaults too) without writing a giant toString that concatenates all the various variables that might look like this:

Object: StateID = 12, Name = "Argentina", CountryID = 32, CityID = 44

My heart tells me this is unlikely.

ale
  • 6,267
  • 6
  • 54
  • 64
Jarede
  • 2,923
  • 3
  • 38
  • 55

2 Answers2

4

This depends on how you are storing your variables within your object. I generally store all of my variables in a variables.instance structure. I then create a get() that simply returns the variables.instance structure:

public struct function get(){
    return Duplicate(variables.instance);
}
Dan Short
  • 9,410
  • 2
  • 26
  • 52
  • 1
    Just a quick note on this. The struct is passed by reference so if you don't want changes to the struct to automatically change the instance variables of the object (usually unwanted) then wrap it in a duplicate call... return duplicate(variables.instance). – Dan Roberts Apr 18 '12 at 15:21
1

If you use cfproperty, and accessor=true or persistent=true in CF9+, a dump of the cfc will return all the properties without getter=false.

Henry
  • 31,972
  • 19
  • 112
  • 214