Correct Me if I'm Wrong But

In order to access a control in one window from a second window, all you need is a reference to the first window, e.g.

Var theText As String = Window1.TextField1.Text

I’ve used this exact method numerous times in the past, but now I’m getting an error:

“This method is protected. It can only be called from within its class
Window1.TextField1.Text”"

Any ideas about what’s going on? Thanks!

Maybe TextField1 Scope in Window1 is private?

2 Likes

Yes, that was it. Interestingly, I went back to a previous project and saw that the scope for all the controls were set to public. They had to be that way by default because I know I didn’t do it manually. Anyways, Many thanks!

In the past controls defaulted to public. Today they either default to the last scope chosen, or they always default to private. I’m not sure which. Keeping controls private is smart because it forces you to write more focused code. Think of each window as its own mini-program with its own inputs and outputs. Window1 should never talk to a control on Window2, it should talk to a method on Window2 instead. This allows you to make changes to Window2 without regard to the code in Window1. For example, maybe you want to swap a ListBox on Window2 for a PopupMenu. You’d just change the code in the method on Window2, and the code on Window1 never changes. This becomes especially useful if you have Window2 and Window3 also calling the same control on Window2. Then you’re changing code one place instead of three.

You don’t have to do this of course, but it’s a very good habit to get into and if Xojo is defaulting to private controls, I am 100% in support of that. I don’t think I’ve used a non-private control in years.

10 Likes

That was a very good description of encapsulation. Well done.

1 Like

Thom, thanks for your reply! Later today I’m gonna try to implement what you’ve said.