OOP best practices

Sorry if this a too basic question, but I am trying to follow OOP rules and there’s something I don’t get clearly…

I have a Xclass with 13 (private) properties and some methods… and I need to display the values of those properties in another window…

So… either the listbox in the other windows has to be public in order to populate it from within my Xclass or the Xclass properties have to be public to be able to get them from the other window, right… ?

Or write 13 public get methods one for each property ?

Am I thinking straight ?

Yes, you have that right, but (without knowing your exact circumstances) I suggest the second option. Or…

If you want, you can create Computed Properties whose getters return the corresponding private properties. That way you can access the properties as read-only externally while having full access internally.

if the properties are PRIVATE then only methods INTERNAL to Xclass can reference them
if you want to refer to some externally, (myXClass.prop) then it must be PUBLIC
and Kems suggestion of using computed properties is a very good one

As an addition

NEVER do this if you want to follow OOP rules. There is something called Seperation of Concerns:
Your Xclass should never manipulate other classes (certainly not GUI controls). This would make it impossible to reuse it in another context (where there is no ListBox, or no GUI at all)
Listbox should ask the data to your Xclass. Xclass shouldn’t care about the format.

Only exception should be a .ToString or .ToXML function (and others like this).

hey, thanks for the replies…

“Listbox should ask the data to your Xclass”… so… how should Xclass “give” the data ?

I read about computed propeties… and I converted all Xclass private properties to computed properties… that’s ok… all get’s and Set’s showed up… but they’re all still private… and not reachable from the other window/listbox/class…

The ‘best practice’ option then (from the oop standpoint) would be to write one public method per property ?

Best practice is set the Computer Properties to Public, let the ‘real’ properties Private/Protected.

With ‘asking’ data I simply mean:

Listbox.AddRow(Xinstance.Name)

The listbox asks the Name of Xinstance and puts that in a row.

Don’t do things like this:

Xinstance.AddRowToListbox(lb As Listbox)

Where the AddRowToListBox method could look like this:

lb.AddRow(myPrivateName)

Both works, but the latter is a bad idea because: What if you want to add a name to a Button, a PDF, WindowTitle?
You could go on and add Methods like:

Xinstance.AddNameToPDF(p As PDFObject) Xinstance.AddNameToButton(b As Button) etc...

I think you sense that this is bad practice. This is ofcourse a exagerated example, it’s easy to fall in the ‘trap’ of giving a class to much responsability.

In Xojo you can switch between a public property, computed property, and get / set methods with out syntax side effects in other users of the class so its not really a hard and fast rule

If you just have get / set computed properties that do nothing more than set some internal value and do no other work then making the underlying property public has few side effects.
And should you need to alter this you can switch to computed or get / set pairs with NO impact elsewhere

Something I wish were true in more languages

I guess I need to read more about Computed proerties…

A bit off topic… is there a way to convert a computed property back to a regular property ?

I right clicked and converted a few… and now it’s too late for undo…

Delete the computed property, rename the backing property and make it public again.

got it !

thanks everybody… the fog is slowly clearing up…

And therein lies the beauty of this aspect of Xojo

In other languages a public property and a get / set pair of methods are syntactically different and such a change would cause pain elsewhere

Not so in Xojo