Hi All,
In my last app, I had many types of objects that I needed. So I created a routine that, based upon the type passed in, would return an object of that type. Made it super easy to enhance an object in one place and easy to reuse.
In Xojo, what we called an object, is a dictionary. So what’s the best way to handle something comparable?
Appreciate,
John…
No not at all, Xojo has classes and objects. Please use them over dictionary whenever possible.
It sounds like you’re looking for a factory function? I usually make these as shared methods on my classes.
Hi Tim,
Oops, I meant to say “…in 4D, what we call an object in Xojo is a Dictionary”.
Do you mean that I should have the class return an object as part of the Constructor? if not, could you provide an example please?
Appreciate,
John…
Constructors cannot return anything - they are called immediately after the object is created so you can set it up with default values, etc. before it is used.
I’m not clear what you’re actually looking for. Can you explain in more detail what functionality you are looking for?
Hi Eric,
First, I am coming to Xojo from a long 4D career. 4D began as a procedural language before evolving to have the ability to program in an OOP way.
Before OOP we did have an Object structure (similar to a Xojo Dictionary). I had a method that I would send a parameter of the return type of object I wanted. So I would initialize an object, with all it’s attributes in this one method, and just call it when I needed it. Kind of a non-OOP factory method, I guess.
So that’s the background.
Since I’m starting Xojo with this long mental history behind me, I am likely approaching things in the wrong way. And probably explaining things the wrong way through a 4D filter.
And as I explain myself, I believe in Xojo it’s best to create a class of the type I need, and have it return an initialized object (or dictionary or???).
I think I’m going to use the class approach since I am in Xojo.
Hope that helps explain…
Appreciate,
John…
I see. Yeah, this is a case where you need to discard the old paradigm and embrace the new. 
Fundamentally, you need to look into Xojo’s class system. Xojo has a pretty well-developed class system and you’ll have a much better experience with the language if you learn how to properly utilize it.
Go read up a little and come back with any questions you have. Describing the entire system is too much for a post but if you have specific questions, we can help.
' Are you trying to manage:
' when the .Value parameter is not known in advance
' or could come from a very long list from which only some .Values will be needed?
' Guessing that Dictionary use is slower than a Class
Var MD As New Dictionary
Var name1, name2 As String
name1="Name"
name2="City"
MD.Value(name1)="Carol"
MD.Value(name2)="Kountze"
name=PickOne("Name Address City St Zip ...") ' could be a long list
MD.Value(name)="Something"
For Each name In Names ' or the convenience
MD.Value(name)=""
Next
' Analogous to:
' MD=New Object
' $name="Tree"
' MD[$name]="Something"
' in place of
' MD.Tree="Something"
Hey Keith,
Thanks for chiming in, fellow 4D developer, as well.
Initially, create the same kind of function call in Xojo as I had in the 4D Method.
For example, say I wanted to create a default object of type ‘GEO_LOCATION’ I might make a call like this:
o_Place:=cObjectFactory_("GEO_LOCATION")
In the method ‘cObjectFactory_’, I had 20 or 30 different types of objects that could be initialized and returned. This was before 4D added Classes and such.
The attributes for a ‘GEO_LOCATION’ object would look like this:
OBJ_Set_Long($cObject; "fk"; -1)
OBJ_Set_Bool($cObject; "_isDirty"; False)
OBJ_Set_Long($cObject; "geoLocation_id"; 0)
OBJ_Set_Text($cObject; "evtDate"; "") // $u200521
OBJ_Set_Time($cObject; "evtTime"; ?00:00:00?) // $_h
//OBJ_Set_Text ($cObject;"evtDate_ut";"") // $u200521
//OBJ_Set_Time ($cObject;"evtTime_ut";?00:00:00?) // $_h
OBJ_Set_Long($cObject; "calTypeNbr"; 0)
OBJ_Set_Text($cObject; "evtDate_jd"; "")
OBJ_Set_Text($cObject; "evtDate_jd_ut"; "")
OBJ_Set_Text($cObject; "country"; "")
OBJ_Set_Text($cObject; "place"; "")
OBJ_Set_Text($cObject; "latitude"; "")
OBJ_Set_Text($cObject; "longitude"; "")
OBJ_Set_Text($cObject; "timeZoneID"; "")
OBJ_Set_Text($cObject; "zoneCode_ST"; "n/a")
OBJ_Set_Text($cObject; "zoneCode_DS"; "n/a")
OBJ_Set_Text($cObject; "zoneCode"; "")
OBJ_Set_Text($cObject; "zoneName"; "n/a")
OBJ_Set_Real($cObject; "gmtOffset"; 0)
OBJ_Set_Real($cObject; "dstOffset"; 0)
OBJ_Set_Real($cObject; "tzShift"; 0)
OBJ_Set_Bool($cObject; "isDST"; False)
OBJ_Set_Text($cObject; "timeType"; "s")
OBJ_Set_Time($cObject; "time_LMT"; ?00:00:00?)
OBJ_Set_Time($cObject; "time_UTC"; ?00:00:00?)
OBJ_Set_Long($cObject; "geoTransitions_id"; 0) // $u181113
So I was thinking I might need something like this in Xojo. But I think the best approach is to spawn what I need from a Class.
That’s the genesis of my question.
I will study your example, as I think (not 100% certain) that a similar data structure to a 4D object is a Xojo Dictionary. Based upon your explanation, it seems that is so? Although dictionaries seem to require a few more steps.
Appreciate,
John…
You definitely want a class to do this. You CAN use a dictionary but you lose a huge swath of the language’s type checking, which is surely why you’re using Xojo to begin with.
Hi Eric,
yes I agree.
I’ll wrap my head around it.
Appreciate,
John…