Getting property values of type integer using introspection... not working

Below is a method which has one parameter “ClassInstance” as object and returns a dictionary. It returns a dictionary with key and value pairs from all properties within the passed class. It returns correct values from the passed class instance for all variable types… except those who are defined as Integer.

For example, property “BUTTON_RADIUS” is property of type Integer with a default value of 9. As you all know, we can define the property as Integer in the IDE. Xojo’s list of VarTypes, does not include Integer though (only int32,int64)

https://documentation.xojo.com/api/language/vartype.html#vartype

I have bypassed this by using IsNumeric to handle those properties which are of type integer, BUT the problem is… p.value(info) (the value of the property being read, returns a nonsense value for all properties of type Integer. I am certain that if I re-define the integer property in the ID to int32 or int64, all will be good… but what’s up with the inconsistency!?

// Return a dictionary array, with all properties, type and values of a given class (info)

// Get the Type info
var info as Introspection.TypeInfo
info = Introspection.GetType(ClassInstance)

// Get property info
Var properties() As Introspection.PropertyInfo
properties = info.GetProperties

// Create a dictionary to hold our collected info
var d as new  dictionary

For Each p As Introspection.PropertyInfo In properties
  // Create a key with the property's name and assign it's value
  d.Value(p.name) =  p.Value(info)
  
Next

// Return the properties and values as as dictionary
return d

Update : Tried changing the property type in the IDE to both int32 and int64, with no results. Returns garbage values, this seems like a bug to me. Will work by setting the type to String, and using ISNumeric can bypass this… but… troublesome.

Because Integer is one of those. Just depends on

#If Target64Bit Then
  // Integer is Int64
#Else
  // Integer is Int32
#EndIf
2 Likes

I do something like this:

For Each p As Introspection.PropertyInfo In properties
  Dim t As Introspection.TypeInfo = p.PropertyType
  Select Case t.Name
  Case "String"
    p.Value(me) = info.Lookup(p.Name,"")
  Case "Integer", "Double", "Int64", "Int32", "Int8", "Int16", "Short", "Uint8", "Byte", "Uint16", "Uint32", "Uint64"
    p.Value(me) = info.Lookup(p.Name,0)
  Case "Boolean"
    p.Value(me) = info.Lookup(p.Name,False)
  Case "Dictionary"
    p.Value(me) = info.Lookup(p.Name,new Dictionary)
  End Select
Next
2 Likes

I like your reply Julia. What type is info? A dictionary? Because my function has parameter ClassInstance (as object) and objects don’t have a lookup method.

Sorry for lack of context. It’s a constructor for populating properties of a class from a dictionary. The class itself is a generic object.

Public Sub Constructor(info As Dictionary)
  Dim properties() As Introspection.PropertyInfo
  properties = Introspection.GetType(me).GetProperties
  
  // This works only for primitives and dictionaries of primitives. No arrays, no deep dictionaries.
  
  For Each p As Introspection.PropertyInfo In properties
    Dim t As Introspection.TypeInfo = p.PropertyType
    Select Case t.Name
    Case "String"
      p.Value(me) = info.Lookup(p.Name,"")
    Case "Integer", "Double", "Int64", "Int32", "Int8", "Int16", "Short", "Uint8", "Byte", "Uint16", "Uint32", "Uint64"
      p.Value(me) = info.Lookup(p.Name,0)
    Case "Boolean"
      p.Value(me) = info.Lookup(p.Name,False)
    Case "Dictionary"
      p.Value(me) = info.Lookup(p.Name,new Dictionary)
    End Select
  Next
  
End Sub
1 Like

Your a rockstar in my books Julia :slight_smile:

1 Like