Dynamic assignment of Property Value based by PropertyName"X"

Hi all! I got a series of Property which differ by the last caracter of their name = number

PropertyName1
PropertyName2
PropertyName3

I would like to exploit a dynamic method which allows me to immediately assign the value of each property in the following way:

// Check Expired or Not Expired

Var RowsFound As RowSet

Try
  
  RowsFound = App.DB.SelectSQL("SELECT Expired FROM Registro")
  
  If RowsFound Is Nil Then Return
  
  For Each Row As DatabaseRow In RowsFound
    
    Dim s As String = Row.Column("Expired")
    
    If s = "Yes" Then
      
      Var n As String = Row.Column("ID").StringValue
      
      PropertyName""+n+"" = "Yes"
      
    ElseIf s = "No" Then
      
      Var n As String = Row.Column("ID").StringValue
      
      PropertyName""+n+"" = "No"
      
    End If
    
  Next
  
  RowsFound.Close
  
Catch Error As DatabaseException
  
  MessageBox("Error: " + Error.Message)
  
End Try

My intent is obviously to cut off the IF STATEMENT that would waste a lot of code when using a great number of PropertyName"n*". Anyway I don’t know if there’s a correct syntax I can use for the current expression:

PropertyName""+n+"" = "No"

I’d be very grateful for any help you can provide me with this.

You can do this with Introspection

https://documentation.xojo.com/api/language/propertyinfo.html#propertyinfo-value

1 Like

Thank you Graham, this seems to be a nice workaround to my problem. My only concern is if I will be able to use the introspection method back while using a Canvas Statement which will determine the active rules for the rendering of objects.

It feels like a Dictionary would be a better fit here.

2 Likes

Thank you Kem, if I’m not wrong you’re saying I’m wasting my time looking for a syntax “workaround”.

I wouldn’t phrase it like that, I’m just offering an alternative that might be a better option for this use case.

1 Like

I could be wrong but reading at the examples of both the Introspection and Dictionary methods it seems they are both based on the listing of the different properties which would be mainly against the concept of dynamic assignment.

If I need to use a CASE why to not use an IF STATEMENT? That’s my point.

Introspection allows you to examine an object and determine, among other things, its properties. A Dictionary allows you to make associations between arbitrary keys and their corresponding values. The former requires you set up the properties you want during design, the latter lets you keys on during runtime.

you clould also use a property array of pair

PropertyName.Add(new Pair(n,“No”))

1 Like

What is your goal?
Is the table “Registro” static or it will grow? you are using ID so if the table grows to 10,000 IDs you will have 10,000 properties?

Maybe if you explain the big picture maybe there is a better way to do what you need/want.

I need to learn more about it, but it’s a nice input, Thank Markus!

The goal as I wrote is to dynamically assign the Value of the Properties without making use of an IF STATEMENT that would lead me to write a long unuseful piece of code. I just got two Values “YES” and “NO” but the number of Properties which I must assign a value to could be variable, surely not 10.000, more likely 50 / 100.

That’s really really interesting Markus, I’m reading now about the way I can assign many pairs of values to the same Property (like the use of digital channels).

My idea is to use then this method in this way:

CANVAS 1

If PropertyName = (1,"Yes") Then

CANVAS RULES HERE

ElseIf PropertyName = (1,"No") Then

OTHER CANVAS RULES HERE

End If

CANVAS 2

If PropertyName = (2,"Yes") Then

    CANVAS RULES HERE

    ElseIf PropertyName = (2,"No") Then

    OTHER CANVAS RULES HERE

    End If

If I can do so this cleverly solve my issue.

Use a dictionary for this.

create a property

Private Property pStoredValues As Dictionary

Dont forget to init it

Public Sub Constructor()
  pStoredValues = new Dictionary()
End Sub

Then add some methods to get and set your values

Public Function PropertyName(index as Integer) As boolean
  Return pStoredValues.Lookup(index,false)
End Function


Public Sub PropertyName(index as integer, assigns value as boolean)
  pStoredValues.Value(index) = value
End Sub


Public Sub PropertyName(index as integer, assigns value as string)
  PropertyName(index) = (value.Lowercase() = "yes")
End Sub

Then using is simple

//Assign values
PropertyName(1) = true
PropertyName(2) = "yes"
PropertyName(3) = "no" 


//get values
if PropertyName(1) then
  //this will run
else
  //this will not run
end if

if PropertyName(3) then
  //this will not run
else
  //this will run
end if
1 Like

Thank you Graham, this is a good lesson to me. However this implements nothing that can be called dynamic: I must create a list of Properties and relative Values. Works but conceptually speaking this is not what I was looking for, hope you’ll forgive me for being so pedant about this aspect but the concept behind it’s almost everything in this circumstance.

Thank you again for your effort anyway!

Another way to approach this:

Create a class and series of subclasses that represent the drawing behaviors you want. As you pull data from the database, you will add an instance of the appropriate class to an array or property.

When it comes time to draw, the Canvas will look to that array or property and tell each instance to draw into it, and there will be no giant IF statements anywhere.

2 Likes

It sounds very similar to the method of SimpleDraw18 If I’m not wrong. I already thought this possibility too but in my opinion it gets the things complex because I need these objects drawn to be used as buttons with animations and actions. XML array I guess it works well but I’m not really confortable with, I must admit this is a personal limit of mine.

I would really like a solution based on a sort of “syntax trick” to allow me keeping things as symple as I can, (As you can see I’m far away to be a code expert).

Thank you Kem