Methods and Modules

I understand it’s good practice to put all your methods inside a module. But when I do this, I find that in order for the window to use the method, I have to include a reference to the window in the method, e.g.

myText = Window1.TextField1.Text

Is there a way around not having to always include a reference to the window in your method? Thanks!

Make the methods Global ?

Thanks for the reply. They are already global.

You can try passing in either data or references to the object as parameters to the method.

If the methods are for manipulating the window and not much more there’s no need to put them in a module. They should be scoped as close to where they’re used.

Global methods should be few and far between.

This is only true for generic methods. If your methods are specific to a window, just leave them there.

Unless of course it’s code that you’ll be using over and over. In that case you could put them in a module and pass references to the controls that you need access to.

Extension methods are good for this. Let’s say you needed a way to encode a text field consistently throughout your app. You could do something like this:

Sub Encode(extends field as TextField)
    Field.text = Uppercase(EncodeHex(field.Text))
End Sub

And then whenever you need that, simply call:

MyField.Encode

That said, something that I grew into over the years was to make Windows and Containers into “black boxes” in that all of the controls are Private and then to add methods for making changes to those things. It makes you be very specific about what external code can change. Think of a progress dialog for instance. Instead of something like:

Window1.Progressbar1.Value = 5

You could have a method:

SetProgress(value as Integer)

That way, things outside of your window don’t need to know about the inner workings and if you decided to add a Label showing the progress as text, you’d only need to change the code in one place.

2 Likes

you could add a argument win as Window1 into your method.
and if your windows have common features give them an interface.
its good to have your business logic in a class.

Thanks, guys! I especially like what Greg said. I was trying to do that exact thing last night but couldn’t figure it out. Much appreciated!