Methods under Window VS App

Hi
I was creating a new method and without noticing I put it under the window control when usually they are under app.
What are the differences ? Is it about scope just like variables ?
So for instance a method placed under Window1 cannot be used if called from an event from Window2 ?
Thanks

It is about scope indeed. You call a public method in app as app.myMethod, but on a window, you call it as Window1.myMethod.

The principle is to always keep methods and variable as close as possible to the place where they are used. If you need a method in Window1, attach it to the window, where you call it by myMethod().

I would refrain from calling a method in a window from another one, unless I am certain Window1 is open. I usually use non implicit windows, and keep the variable in app or in a module. For instance Win1 as Window2.

To display the window, I will do

app.Win1 = new Window1

That enables testing if the window is displayed, with

if app.Win1 <> nil then

It’s all about scope, yes. If Window1 is not opened before Window2, and you call Window1.mymethod, I think Window1 will be forced to open to be able to access the method you call.

But if the function actually doesn’t belong to Window1, because of what it does, I would move it to App.

Only if implicit instantiation is on. Otherwise it triggers a nil object exception.

As a matter of principle, I would not place a function that should be called from another window on Window1. I would rather place it in app or in a module.

This should not be your standard practice. If you are routinely placing your methods within the App scope because you’re not really sure where they should go, then you need to redesign/rethink your app’s design. Good OOP practice is to place methods, variables, properties, etc within the most restrictive scope under which they can function.
If a method deals only with one window, place it in that window’s scope. If you need a one-time variable from another window, have your window1 retrieve the variable without calling window2’s name. (See discussions on event definitions for an good way of doing this)
When you, or someone else, return to this app months or years from now, you will thank yourself for the encapsulation you took the time to create.

So that’s why… thanks for the info!