Any way to get a window instance given it's name?

Hi folks,

given a window class name, is it possible to get an instance of that window in return ?
it would greatly simplify one of my method, instead of a huge bunch of select…case.

thanks.

Sure, you can use isa:

Dim u As Integer = app.WindowCount-1
For i As Integer = 0 To u
  dim v as Variant = app.Window(i)
  If v IsA Window1 Then
    Dim w As Window1 = v
    w.Title = "found"
  End If
Next

Can you post a short example of your existing code, just to make sure we’re on the same page?

@Christian_Schmitz : I have a window class name as a string, and Window1 is not a string, xojo speaking… oh and the windows are not already opened.

@Kem_Tekinay : sorry it’s in my head for now, searching for ideas to simplify it.

I get a window class name in a string, coming from inside a text file
I want to generate the window instance having that name, among the dozens of different windows the app has, without using a big select…case with one case for each window class.
is it possible ?

may be using runtimeiterator and testing any window.name with the given string ?

Yes, through Introspection.

You have to register the string against the window’s TypeInfo somewhere, then lookup the string to get that TypeInfo. With that, you can use GetConstructors and Invoke to create and Show a new instance.

1 Like

thanks @Kem_Tekinay will try this.

I hate to rain on the parade, but just use the select statement. I originally went the route of using introspection, using something like

Var Types() As Introspection.TypeInfo
Types.Add(Introspection.GetTypeInfo(ObjectA))
Types.Add(Introspection.GetTypeInfo(ObjectB))
Types.Add(Introspection.GetTypeInfo(ObjectC))

And looping through the array until I found the match. Logically, this array was cached at launch, but that’s besides the point. This worked 99.9% of the time. But every once in a while a customer would report something illogical: some of the objects were missing. It made no sense, but sure enough, they were just… missing. Reinstalling the program (on Windows, never reported on Mac) would help, until a later launch the same ones would disappear again. Same version of the app, code signatures all intact… it makes no sense. This wasn’t dead code stripping, because that would happen for all users.

I switched to the less elegant select statement, and the problem is gone. In my experience, introspection for mission-critical functionality will lead to bizarre and hard to track down errors. I still use it in places, but I’ve scaled back my reliance on it a lot. I’m happier with the stability than I am the code elegance.

Counterpoint: we have been using a scheme like the one I outlined for years for data serialization between a middleware app and a desktop GUI without issues.