create a class instance by it's name ?

is it possible ?
I want to create a menubar instance (associated to a window)
but I want to create it from a property “mywindowmenubar” that is a string with the name of the menubar “menubarofmywindow” in it

is it possible, with introspection or something ?
would be nice…
thanks.

make a factory method taking class name and a big select case statement.

select case ClassName case "Test" return new Test ... end select

already tried this way - thanks Christian.
but there are methods I need to write then than can make full abstraction of the original class
and if I use your method I must instantiate the class and so make it specific to the application beeing made
cannot be externilized after.
good exemple is a menubar with regular or special commands in it
or windows that are present in multiple applications.( but sometimes with different names…)

[quote=167588:@jean-yves pochez]already tried this way - thanks Christian.
but there are methods I need to write then than can make full abstraction of the original class
and if I use your method I must instantiate the class and so make it specific to the application beeing made
cannot be externilized after.
good exemple is a menubar with regular or special commands in it
or windows that are present in multiple applications.( but sometimes with different names…)[/quote]

If I understand you right you would want to dim a new menubar something like :

Dim "MyNewMenuBar" as MainMenuBar

I remember a thread a while ago where a Xojo engineer explained a class had to be instantiated with a literal. Of course typing literal in the forum brings much too many results, so I could not point to it.

There is a way, though. Add a dictionary to a module or to the window and do this :

d = new dictionary dim m as new MainMenuBar d.value("mybar") = m self.MenuBar = MenuBar(d.value("mybar"))

You can create as many instances as you want with strings.

[quote=167588:@jean-yves pochez]already tried this way - thanks Christian.
but there are methods I need to write then than can make full abstraction of the original class
and if I use your method I must instantiate the class and so make it specific to the application beeing made
cannot be externilized after.
good exemple is a menubar with regular or special commands in it
or windows that are present in multiple applications.( but sometimes with different names…)[/quote]

Instead of a select case statement that creates instances, you could have the select case statement return introspection TypeInfo objects. These objects give you a lot of metadata about a class and the ability to construct instances on the fly. Here’s a small example:

Function LookupType(name As String) As Introspection.TypeInfo Select Case name Case "MenuBar" Return GetTypeInfo(MenuBar) Else Raise New KeyNotFoundException End Select End Function

As for the reason that you can’t arbitrarily create an instance for a string: the compiler’s linker does dead code stripping. Any class that isn’t explicitly referenced via New, IsA, GetTypeInfo, or introspection metadata will be stripped out of the final binary.

well this should be implemented…

the aim now for me is to add a menubar when a window opens (or activate) and close when the window closes or desactivates.
the menubar belongs to the window
I want to make a property to the window that holds the name of the menubar
as I cannot enter a class instance in the property editor in the ide when I edit the window, only strings or simple types.
then in the activate event of the window, if I only have the string, I cannot create a new instance of the menubar
without a big select…case that will grow for each application I make.
this is for windows that will be in many different applications.
I dont see a simple implementation for that.

[quote=167629:@jean-yves pochez]well this should be implemented…

the aim now for me is to add a menubar when a window opens (or activate) and close when the window closes or desactivates.
the menubar belongs to the window
I want to make a property to the window that holds the name of the menubar
as I cannot enter a class instance in the property editor in the ide when I edit the window, only strings or simple types.
then in the activate event of the window, if I only have the string, I cannot create a new instance of the menubar
without a big select…case that will grow for each application I make.
this is for windows that will be in many different applications.
I dont see a simple implementation for that.[/quote]

No need to create a property to the window that holds the name of the menubar. It already exists : Window.MenuBar http://documentation.xojo.com/index.php/Window.MenuBar

You did not write a word about the dictionary technique I described three posts above. Have you seen it ? It would allow you to create as many menubar as you want at any time with a string, and have it attributed to the window. It allows you to customize it before you store it, and reuse it in activate as many times as you wish if you place the dictionary in a module. And no need for cumbersome select case…

If the issue is to create a customized instance of the menubar specific to the window,

  • add a menubar property to your window. For instance ThisWindowMenuBar as MenuBar
  • In the window Open event :

Sub Open() dim mymenu as new MainMenuBar mymenu.append(New MenuItem("Import")) // Customize for use ThisWindowMenuBar = mymenu self.MenuBar = ThisWindowMenuBar End Sub

  • In the Activate event :

Sub Activate() if ThisWindowMenuBar <> nil then self.MenuBar = ThisWindowMenuBar end if End Sub

thanks Michel that answered my actual problem with menubars in windows
it’s been solved because you can put an instance of a menubar in the window ide
and you’re not forced to use a string there.
but it would still be nice to be able to instanciate a class by its name as a string
sort of reverse of introspection.

Review this discussion: https://forum.xojo.com/19932-instantiating-an-object-with-a-classname