Why use dim MyWindow as New <xxx> ?

I have been opening Windows via a PushButton using .Show

Let’s say is going to be Programs

What is the difference between that and using …
Dim MyWindow As New Programs
MyWindow.Show

If you use dim mywindow as new programs then you can create multiple copies of the window. You would use this, for example, if you had an editing or viewing window for a certain class and you wanted to have the capability to open more than one at a time.

This has to do with whether the window’s Implicit Instance property is set to true.

By default all windows are implicit windows. What this means is that you don’t need to (and, in fact, cannot) instantiate the window with the New keyword. Implicit windows are instantiated as soon as they’re used and there will only ever be one instance of the window. You can use an implicit window just by calling it by name:

Sub ShowMyWindow()
    <WindowName>.Show
End Sub

For non-implicit windows, you must use the New keyword every time you want to use the window, and there can be multiple independent instances of the window simultaneously:

Sub ShowTwoMyWindows()
    Dim win1, win2 As <WindowName>
    win1 = New <WindowName>
    win2 = New <WindowName>
    win1.Show
    win2.show
End Sub

Thanks guys … I got it now.

[quote=42084:@Andrew Lambert]This has to do with whether the window’s Implicit Instance property is set to true.

By default all windows are implicit windows. What this means is that you don’t need to (and, in fact, cannot) instantiate the window with the New keyword. [/quote]

Incorrect
You can always create a new instance with New - just with implicit instantiation you can also just use it without explicitly creating one first.
Even on a window with implicit instance set to true you can do

dim w as new Window1
w.show
Window1.show

and you should have 2 window1 windows showing

It seems you can’t refer to a non-implicit window without using New, but the opposite is not true. I coulda sworn…

with an implicit window if you don’t already have it open an instance will be created and then every use of the implicit instance uses the same instance.
But you can create more but you can’t refer to them using the implicit instance style.
A reference needs to exist somewhere - even if its just in the list of windows the runtime maintains.
You don’t explicitly HAVE to maintain a reference but sometimes its handier

With NEW you get a fresh, pristine copy each time. With implicit instance, you may be accumulating cruft if for example you have a circular reference somewhere. That could lead to crashing.

OK, so if I’m opening a window using, for example, Programs as the windows name in code: Programs.show. Then, if I go open another window called Offices the same way (Offices.Show) and then when I close the Offices window and issue another Programs.Show … what exactly is happening? … am I creating another instance or am I just “showing” the already created instance? Is this method accumulating any cruft ?

Asking these questions coz I am trying desperately to find a resolve for the RBGUIframework.dll crashing I am getting. Just poking at straws in hopes of finding something.

If you have a window as an implicit instance and use programs.show, it will reference the same instance of the window every time. If you use programs.show , then use offices.show you will have both windows open, and calling programs.show again will simply bring that window to the front.

When you close a window, whether implicitly or explicitly created, the framework attempts to destroy each of the controls and any objects associated with properties of the window. However, there are things you can do in code that prevent some of that from happening. In that case, you wind up with a partially destructed window. When you use an implicit window, some variables related to the creation of the window may be reused. That is where I see a potential problem arising. It isn’t clear how “fresh” the new window really is. In my opinion, it would be worth trying to use explicit windows instead of implicit windows.

An easy way to make the change over is to rename the window in the IDE and then add a global property with the original name. Eg., instead of an implicit window named Programs, change the name to ProgramsWindow and add a property

Programs as ProgramsWindow

Then when you show it, use

Programs = New ProgramsWindow
Programs.Show

I don’t know if this will help at all, but it’s worth a shot.

Tim … I will definately give this a try.

Where exactly should I create the global property… in the App?

In a Module. If you put it in App, you still have to change all your code from Programs to App.Programs.

Set it up in a module … tried it on one window. Works [ in theory ] but the problem then becomes when that window is referenced elsewhere I have to re-create it using “Programs = New ProgramsWindow” and then I wind up with multiples of it. That won’t work.

Why would you have to do that? You only need to New it when you want to Show it. Anyway, it is kind of a long shot and may not be worth a lot of effort to get it working.

Example, the Programs window displays a list of “programs”. Double-clicking one of them opens another window called Address. Changing a value on the Address window then requires re-population of the Programs window, while the Address window is still open. Code in the address window calls code in the Programs window and then I get a new instance of it.

Maybe I’m doing something wrong but if I don’t add “Programs=new ProgramsWindow” in the Address window before the code Programs.RefreshList then the thing bombs. (RefreshList is code I use to refresh information on the Programs window)

How does it “bomb”? NilObjectException? And do you have both the Programs and Address window open? Or did you close the Programs window when you opened Address?

If I kept it as simple as that, things would work but unfortunately my app isn’t simple. For example, I have a common subroutine in a module that is called by almost every window in my app. The routine is passed parameters based on which window is calling it. It then determines what to do. If I don’t put in that routine “Programs=new ProgramsWindow” it won’t compile because it doesn’t yet know what that is. If I do put that statement in there, it will open that window each and every time that routine is called.

You shouldn’t be getting a compile error if Programs is a global property. You might get a runtime error if Programs is nil, but it should still compile.

ahhhhhhh… I must have did something wrong coz after reversing it back, I did it again and then it was ok.

anyhoo … see my latest post on the RBGUIframework thread … it appears as though I may have resolved the issue. (they don’t call me “grinder” for nothing LOL)