I have a Class that has over 150 subclasses (implementing XML spec), is there a way to do the equivalent of “Var myItem As MySuperclass = New SpecificSubclass” generically without hardcoding all the classnames in a long ElseIf or Select Case? My goal is to keep a list of XML Property names and the corresponding specific Subclass in a Xojo.Core.Dictionary (or something similar)
You can write an app to write Xojo classes as text files and drop them in the class.
I may not have explained what I need clearly enough. I’m looking to replace a Static Method’s 150+ option long “If theName = exactSpecifier Then Var myItem As MySuperclass = New SpecificSubclass ElseIf theName = AnotherSpecifier . . .” (or the equivalent Select Case) with a Generic "Var myItem As MySuperclass = New " I tried “dict(“specifier”) = ExactSubclassname” then “Var myItem As MySuperclass = New dict(“specifier”).Value(constructor parms)” but it didn’t work.
Okay, use introspection to get class by name and then call constructor.
I looked at the docs for Introspection but couldn’t see what you’re referring to. Can you please post a simple example?
You get the class, get the ConstructorInfo objects, pick one and call Invoke on it.
All the docs only show the example of a parameterless constructor call but the constructor I need to call has one parameter. Could you post an example of calling a constructor with a single parameters please? In my actual code, the parameter is an object.
I just realized that the class MAY HAVE two constructors as the Super class has a Private no-arg constructor to stop it from being instantiated. In this case, will it automatically call the correct constructor if I pass the parameter? If not, how do I make sure I’m calling the proper constructor?
Invoke takes optionally an array of variants for parameters, so pick the right constructor and pass the required parameters.
Where can I put my test Project so you or someone else can explain the “expect Variant() but this is Window1.Window1” error message?
Uploaded “Introspection Test” to https://www.dropbox.com/s/o8xunilf80hp2f7/Introspction%20Test.xojo_binary_project?dl=0
Any and all assistance welcome.
[quote=493260:@Bryan Dunphy]Uploaded “Introspection Test” to https://www.dropbox.com/s/o8xunilf80hp2f7/Introspction%20Test.xojo_binary_project?dl=0
Any and all assistance welcome.[/quote]
you are welcome