[Gambas-user] Request for advice

T Lee Davidson t.lee.davidson at gmail.com
Tue Jul 13 21:51:08 CEST 2021


On 7/13/21 11:43 AM, John Dovey wrote:
> Now I'm looking at one of the objects. (user) and setting the Property
> for "photo" which is another object called chatphoto.
> How do I implement this?
> 
> I've got
> ---
> tb_type_chat.class
> 
> Export
> Property id As Long Use $id
> Property type As String Use $type
> ' etc then
> Property photo As Tb_type_chatphoto Use $tb_type_chatphoto '<- This
> obviously refers to a different object. in the Read function, I guess
> I populate the new object?

It would be preferable to populate the object in its _new() method as opposed to its Read() method.

Let's assume that, using the tb_interface module, you have retrieved a Chat (JSON) object from the API server (with the getChat 
method?), decoded it to a JSONCollection, and returned it to the Main module in a JSONCollection called "jsonChat".

You could populate your Chat object with just:
Dim oChat = New tb_type_chat(jsonChat)

The _new() method of tb_type_chat could be, in part:
[code]
Public Sub _new(cChat as JSONCollection)
$id = cChat["id"]
$type = cChat["type"]
[snip]
$photo = New tb_type_chatphoto(cChat["photo"])
[snip]
End Sub
[/code]

The _new() method of tb_type_chatphoto could be:
[code]
Public Sub _new(cPhoto as JSONCollection)
$small_file_id = cPhoto["small_file_id"]
$small_file_unique_id = cPhoto["small_file_unique_id"]
$big_file_id = cPhoto["big_file_id"]
$big_file_unique_id = cPhoto["big_file_unique_id"]
End Sub
[/code]


This highlights one thing to me. Since JSON.Decode converts the JSON object into its appropriate Gambas type (most often 
collections), it actually might be much simpler to let the JSON component (and JSONCollection) handle the data structure instead 
of going through the pain (and possible confusion) of converting/transferring the data to objects. In that case, accessing the 
big_file_id of a Chat object's ChatPhoto object would simply be jsonChat["photo"]["big_file_id"].

And, personally if it were me, I actually might do that since I prefer simplicity over strict OO adherence. But as I said, 
others prefer objects. Pick your poison.


-- 
Lee


More information about the User mailing list