Set instance property by name (at runtime)

  1. 9 months ago

    Ben B

    is not verified 19 Mar 2017 Perth, Australia

    Hey guys,

    how can I set a property of an instance when I have the property name saved in a variable?

    // Car class is defined to have the property "interior_color" (String)
    
    Dim vw_golf As New Car
    Dim car_property As String  = "interior_color"
    
    //how can I set the property without naming it explicitly in the code?
    vw_golf.value(car_property) = "black"
    

    Thanks guys!

    Thanks for the hints!

    I got it to work with PropertyInfo.Value

    1. introspection
    2. dont use properties

    its more work but it has a ton of flexibility
    you create a class that can represent a property (it has a name, type, value etc) and then every one of your classes can have an array of these

  2. Kem T

    19 Mar 2017 Pre-Release Testers, Xojo Pro New York

    Another alternative is Operator_Lookup . Or just use a Dictionary.

  3. Ben B

    is not verified 23 Mar 2017 Answer Perth, Australia

    Thanks for the hints!

    I got it to work with PropertyInfo.Value

or Sign Up to reply!