Xojo2024R2: class name redirection - is it possible?

I have “rep_000” class, it is used as a super class for “rep_175”, “rep_190”, etc.
I have this code:

If mCurrentQuery.saved_query_id = 175 Then
  mRepObj = New rep_175
ElseIf mCurrentQuery.saved_query_id = 190 Then
  mRepObj = New rep_190
ElseIf mCurrentQuery.saved_query_id = 214 Then
  mRepObj = New rep_214
ElseIf mCurrentQuery.saved_query_id = 246 Then
  mRepObj = New rep_246
  mRepObj = Nil
End If

I would like to refactor using redirection like in this pseudo-code:

 mRepObj = New rep_[mCurrentQuery.saved_query_id]

Is this possible in Xojo? If so, how would I go about it?

In the meantime I have asked chatGPT for some help, here is what it said:

In Xojo, you can achieve this kind of dynamic instantiation using a Dictionary to map the saved_query_id to the appropriate class type. This allows you to avoid a long series of If...ElseIf statements and makes your code more maintainable.

Here’s how you can do it:

  1. Create a Dictionary to map the saved_query_id values to the appropriate class constructors.
  2. Use this Dictionary to instantiate the correct object based on the saved_query_id.

Where do I fin an example of that? Would it work with latest Xojo?

That answer from ChatGPT is technically correct, but not any more efficient than what you’re already doing. You’d define a class constructor delegate:

Delegate ClassConstructorDelegate() as variant

and then populate a dictionary with pointers to class constructors:

ClassConstructorLookup.Value("class1")=AddressOf Class1.CreateInstance
ClassConstructorLookup.Value("class2")=AddressOf Class2.CreateInstance

…where each of the CreateInstance methods is a shared method on each class that returns an instance of the class:

Shared Function Class1.CreateInstance() as Class1
    Return new Class1

…and call it like this:

newClassInstance= ClassConstructorDelegate(ClassConstructorLookup.Value("nameOfClass"))

There are good reasons to do it this way; for example, serialization/deserialization systems can make use of this functionality to instantiate classes in a generic fashion. Otherwise, there’s no advantage to all this baroque code so just use your Select Case className structure and be done with it.


Eric, thanks for sharing your knowledge. I will stick for now to my Select Case structure as I still need to digest what you said.

The purpose of my question was to get something easy to work. Right now as I am rewriting the old report page to the new one I have about 70 reports, each one will end up as “rep_xxx” class for now. The idea was to add new class without working about any dependencies and have this just work, particularly important some time in the future when adding more reports.

I may however move the code stored in “rep_xxx” into database itself and use queries instead of methods (to return list of columns or build queries dynamically for RowData, RowCount in the DataSource class of the WebListBox control used on the WebPage). Thanks again.

Anyway, thanks again.

you may need a interface and assign it to the report classes