I’m finally getting to trying to make better use of Classes and Objects rather than just making Xojo work like a procedural, or structured, language.
Here’s the scenario:
I have a Global module that contains a class clsContact which has appropriate contact type properties
I have a Global method (not a method of the class but separate) CreateContactObject with an parameter of a recordset with one record in it
The method instantiates oContact from clsContact and populates the properties from data read from a SQLite table in the recordset
So local methods call CreateContactObject(recordset) and then use the oContact object
My questions is can the CreateContactObject method be a method of the Class? Or does it have to remain outside the class?
Use a constructor with the recordset as parameter.
Hi William, think it would be good to check out these:
You create an instance of a class, to be called an object. The class cannot instance itself, so some code outside the class must create one or more instances of it, references can be kept in an array or dictionary, and do something with it.
Thanks. This answers my question exactly and thanks for the references. I will read
Not sure what happened but I click on the link in Joost’s comment and it seems broken. Found the same link (cut and pasted above) by going to developer.xojo.com and found the content.
so the url is correct but the link itself seems broken… I right-clicked the link and inspected the element and the actual code points to example.com
You can make it a Shared method of the class (move it to the class, right click->“convert to shared”). Shared methods are global and static like module methods, but have access to the private methods and properties of the class.
Dim contact As clsContact = clsContact.CreateContactObject(rs)
Or, maybe just:
Dim contact As clsContact = clsContact.Create(rs)
Or turn it into a Constructor method like Eli suggests.