On Oct 03, 2006, at 10:23 AM, Kevin Windham wrote:
Is there a smart way to access an object and it's properties from a
string.
Suppose I have an object of class Car and properties of weight and
maxSpeed. If I read in a config file that has some config
parameters for this car,
weight: 5000
maxSpeed: 248
along with a whole bunch of other parameters, is there a decent way
to set up my code to fill those parameters in without using a bunch
of case of if statements?
i.e.
case paramstring = "weight"
weight = paramstringvalue
etc. for pages and pages.
Make your class have two dictionaries
originalValues as Dictionary
modifiedValues as Dictionary
Add a constructor and make the two dictionaries when you create a new
instance
Add two methods
ValueForKey(keyName) as Variant
if modifiedvalues.HasKey(keyName) then
return modifiedValues.value(keyName)
else
if originalvalues.HasKey(keyName) then
return originalvalues.value(keyName)
else
return nil // maybe raise a key not found
exception instead ?
end if
end if
TakeValueForKey(keyName as string, value as variant)
modifiedValues.value(keyName) = value
(or you can use Operator_lookup ... your choice)
The down side to this is that you get little compiler help verifying
the code doesn't try to access a property that does not exist. That's
the down side of late binding whether it's properties or libraries.
One thing you can do is make simple computed properties on top of
your classes so they use the above mechanism, but the computed
properties are the only thing exposed to the public.
This way you can write one super class and then make custom
subclasses that all share the super class' implementation and all you
do is add setter / getter computed properties.
That way you get ease of set up and some compiler checking.
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>