Understanding Instances of Window

Hi,

After reading about how using Implicit Instance is such a bad thing, I trying to understand how windows work when they are manually instantiated.

I have a fresh project with no code in it except the following.

I changed the following properties on Window1:

Visible = False ImplicitInstance = False

In App.Open:

Sub Open() dim w1 as new Window1 End Sub
A global property in App

w1 As Window1

When I run the project, the debugger shows:

Window1.Window1 Window1.Window1

Why? Shouldn’t I only see the one instance of Window1 that I created, that is w1?

Also, Window1’s .Visble property is False in the IDE and I never use w1.Show in code, so why is the window shown when the app opens?

Thank you.

A few things I can see:[quote=44622:@Mark S]Sub Open() dim w1 as new Window1 End Sub
A global property in App

w1 As Window1
[/quote]
You have created 2 different variables, a global one in the App, and a local one in the Open event. They are not the same. Remove the ‘Dim’ statement in your ‘Open’ event
]Sub Open() w1 = new Window1 End Sub
Also, is ‘Window1’ the default window in the app’s properties? It will automatically open a new window instance on startup, and you are opening a second one in the Open event.

There can be another question; why:

Window1.Window1

Thanks Mark. I understand my two variable mistake. That’s what I get for copy and pasting.

Window1 is the default window. So does this mean the default window cannot be manually instantiated?

However, I set up a second window in the IDE named, Window2.

I changed the following properties on Window2:

Visible = False ImplicitInstance = False Placement = 2 - Main Screen

In App.Open:

Sub Open() w2 = New Window2 End Sub

A global property in App:

w2 As Window2

Now with these settings the window, w2 doesn’t open, but in the debugger under “Globals” -> " Windows", I still see:

Window1.Window1 Window2.Window2

Shouldn’t, “Globals” -> " Windows" display:

Window1.Window1 w2

In the debugger, under the App properties, I see:

Name: w2 Value: Window2.Window2

Shouldn’t App properties display:

Name: w2 Value: Window2

Thank you

[quote=44624:@Emile Schwarz]There can be another question; why:

Window1.Window1[/quote]

Windows are, in reality, MODULES that contain a CLASS so we can do things like make implicit instances work
So this is correct

Ah HA! They are really modules. That explains it. Thank you Norman.

I have two more questions.

  1. From above: [quote]Window1 is the default window. So does this mean the default window cannot be manually instantiated?[/quote]

  2. Assuming w2 has been explicitly instantiated and w2 is holding the last reference to an instance of Window2 … After executing self.Close, why does a check on w2 (it’s last reference) not Nil?

Thank you

If you set the App’s default window property to NONE then in the open event you can instantiate whatever window you want
When you set it in the IDE that just does it automatically when the IDE opens

How are you checking this ?

Very simply, on PushButton1 of Window1 I have:

Sub Action() If w2 = Nil Then // If there wasn't one previously then we need to create one w2 = New Window2 // Creates a reference to a new Window2 window. End If w2.Show End Sub

In the Window2 Close event I have:

Sub Close() self.Close End Sub

When I click on PushButton1 the very first time, w2 shows. After I close w2, any clicks on PushButton1 will not show w2.

EDIT: To clarify, after the first click it’s not Nil, so it never creates a new instance. However, w2.Show does not show anything.

Thank you.

Closing a window does not set it to Nil. What are you trying to accomplish?

In this simple case, I’m trying not to open up a new w2 if one already exists. I guess you can’t do that reliably by checking Nil then.

Although, it could never happen in this simple app, I wouldn’t want multiple w2’s open and shown to the user at the same time.

[quote=44729:@Mark S]In this simple case, I’m trying not to open up a new w2 if one already exists. I guess you can’t do that reliably by checking Nil then.

Although, it could never happen in this simple app, I wouldn’t want multiple w2’s open and shown to the user at the same time.[/quote]
AH
Try checking to see IF an instance already exists with

For i as integer = 0 toWindowCount-1
 if window(i) isA Window2 then
    // there is one already so show it
    window(i).show
    return
  end if
next

// there isn't one
// so do whatever you need 

Thank you