Introspection - how to get the value of a property in an instantiated class ?

Greetings all,

I have a class (dbEntry) which contains a bunch of properties; for this conversation it will have three = properties

  • name ( string)
  • street (string)
  • gender (string; M or F)

The class is instantiated in code and given values such as:-
dim ne as new dbEntry
ne.name = “Jim”
ne.street = “Avon”
ne.gender = “M”

Now I want to discover the properties in ne automatically, so that if I add another new property to dbEntry some time later, my code will deal with the new property without me having to write out in the code editor a list of all the properties in the class.

So I do this:
dim n as string
dim neProperties() as Introspection.PropertyInfo = Introspection.getType(ne).getProperties
for i as integer = 0 to uBound(neProperties)
n = neProperties(i).name
//here lives a function to write the name of each property of ne to a spreadsheet
next

This produces a list of the names of the properties in the object ne. All good.

Now I want to obtain the values of the property in this specific instantiated class. And here is where I have trouble.
What magical syntax allows me to find the value of ne.street ? (i.e." Avon") – when the code don’t ahead of time know that ne has a property named street ?

Introspection gives me the name of the new property ne.street … How do I get ne.street’s value ?

Note - street does not appear to be an “attribute” of ne, as mentioned in the LR for Introspection. street is a property of ne. Attributes seem to something else, only available by jumping through some hoops in the IDE.

Regards,
Tony Barry

First, please use the Code tags to format your code. It makes it easier for us to read that code and help.

In your loop:

for ...
  dim p as Introspection.PropertyInfo = neProperties( i )
  n = p.Name
  dim v as variant = p.Value( ne )
  ...

The array of properties belongs to the class, not the instance, so the Value function gets the property’s value from the instance you specify.

Also see:

http://developer.xojo.com/introspection