[Rename] Object-oriented programming simulation for a virtual object manager

Just starting out? Need help? Post your questions and find answers here.
SMaag
Enthusiast
Enthusiast
Posts: 302
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: [Rename] Object-oriented programming simulation for a virtual object manager

Post by SMaag »

You nearly got it!
But you do not need
*Methods.ObjectMethods
The Methodes are all listed in the Interface, so you do not need any extras for this!

I modified your code a little to show the difference between Module call and Interface call

Code: Select all


DeclareModule MyObj
  ; Code créer par Dieppedalle David avec l'aide de l'IA, le 22/02/2025.
  ; Code created by Dieppedalle David with the help of AI, on 22/02/2025.
  
  ; ======================================================
  ; Structure des propriétés de l'Objet.
  ; Structure of the Object's properties.
  ; ======================================================
  Structure ObjectProperties
    ID.i    ; L'ID de l'Objet              |   The ID of the Object.
    Name.s  ; Le nom de l'Objet.           |   The Name of the Object.
    X.i     ; La coordonnée ou valeur X.   |   The X coordinate or value.
    Y.i     ; La coordonnée ou valeur Y.   |   The Y coordinate or value.
  EndStructure
  
  ; ======================================================
  ; Interface pour les méthodes de l'Objet.
  ; Interface for Object methods.
  ; ======================================================
  Interface ObjectMethods ; Les Noms des procédures doivent être identiques à ceux du code, avec les mêmes types et les mêmes paramètres.
                          ; The names of the procedures must be identical to those in the code, with the same types and the same parameters.
    Abc123.i()
    Abc246.i()
    AbcXY.i()
    AbcStr.s()
    AbcBox.s(Title.s, Message.s)
    DeleteObject.b()
    GetProperties.i()
   
  EndInterface
  
  ; ======================================================
  ; Structure principale de l'Objet.
  ; Main Object structure.
  ; ======================================================
  Structure StructureObject
    *VirtualTableObject           ; Pointeur (*VirtualTableObject) sans type, qui pointe vers l'adresse des procedures.      |   Pointer (*VirtualTableObject) without type, which points to the address of the procedures.
    Properties.ObjectProperties   ; (*Properties) de type ObjectProperties qui pointe vers les propriétés (état).   |   Pointer (*Properties) of type ObjectProperties that points to properties (state).
  EndStructure
  
  ; Make the Methodes public to call it directly
  ; For Interface call it can be private
  Declare.i Abc123(*Object.StructureObject)
  Declare.i Abc246(*Object.StructureObject)
  Declare.i AbcXY(*Object.StructureObject)
  Declare.s AbcStr(*Object.StructureObject)
  Declare.s AbcBox(*Object.StructureObject, Title.s, Message.s)  
  Declare.b DeleteObject(*Object.StructureObject)
  Declare.i NewObject()
  Declare.i GetProperties(*Object.StructureObject)

EndDeclareModule

Module MyObj
     
  Global NextObjectID.i = 1
  
  ; ======================================================
  ; Des procédures quelconque.
  ; Any procedures.
  ; ======================================================
  Procedure.i Abc123(*Object.StructureObject)
    ProcedureReturn 123  ; Retourne la valeur entière 123.   |   Returns the integer value 123.
  EndProcedure
  
  Procedure.i Abc246(*Object.StructureObject)
    ProcedureReturn 246  ; Retourne la valeur entière 246.   |   Returns the integer value 246.
  EndProcedure
  
  Procedure.i AbcXY(*Object.StructureObject)
    ; Si l'Objet est valide.   |    If the Object is valid.
    If *Object
      ProcedureReturn *Object\Properties\X + *Object\Properties\Y
    Else
      Debug "Error: Object Or properties Not initialized."
      ProcedureReturn 0
    EndIf
  EndProcedure
  
  Procedure.s AbcStr(*Object.StructureObject)
    ; Si l'Objet est valide.   |    If the Object is valid.
    If *Object
      ProcedureReturn "X + Y = " + Str(*Object\Properties\X + *Object\Properties\Y)
    Else
      Debug "Error: Object Or properties Not initialized."
      ProcedureReturn ""
    EndIf
  EndProcedure
  
  Procedure.s AbcBox(*Object.StructureObject, Title.s, Message.s)
    ; Affiche une boîte de message avec un titre et un message, puis retourne une chaîne de confirmation.
    ; Displays a message box with a title and message, then returns a confirmation string.
    MessageRequester(Title, Message)
    ProcedureReturn "Message displayed !"
  EndProcedure
  
  ; ======================================================
  ; Détruit un objet et libère toute sa mémoire allouée.
  ; Destroys an Object and releases all its allocated memory.
  ; ======================================================
  Procedure.b DeleteObject(*Object.StructureObject)
    
    ; Vérifie si l'objet est valide (non-nul)   |   Checks if Object is valid (non-null).
    If *Object
      
;       If *Object\Properties 
;         FreeStructure(*Object\Properties) ; Libère les propriétés   |   Frees properties.
;       EndIf
;       
;       If *Object\Methods 
;         FreeStructure(*Object\Methods) ; Libère les méthodes   |   Frees methods.
;       EndIf
      
      FreeStructure(*Object) ; Libère l'objet principal   |   Frees main Object.
      
      ProcedureReturn 1
      
    Else
      Debug "Error: Object Not initialized: " + Str(*Object)
      ProcedureReturn 0
      
    EndIf
      
  EndProcedure
  
  Procedure,i GetProperties(*Object.StructureObject)
    ProcedureReturn  @*Object\Properties
  EndProcedure
  
  ; ======================================================
  ; Création d'un nouvel Objet.
  ; Creating a new Object.
  ; ======================================================
  Procedure.i NewObject()
    
    ; Déclare un Pointeur (*Object) protégé de type StructureObject, qui prend comme valeur l'adresse de la structure "StructureObject".
    ; Ensuite, alloue un nouvel Objet de type Structure dynamique.
    ; 
    ; Declares a protected Pointer (*Object) of type StructureObject, which takes as a value the address of the "StructureObject" structure.
    ; Then, allocates a new Object of dynamic Structure type.
    Protected *Object.StructureObject = AllocateStructure(StructureObject)
    
    ; Si l'Objet *Object est bien initialisé.
    ; If the Object *Object is properly initialized.
    If *Object.StructureObject
      
      *Object\VirtualTableObject = ?VirtualTableObject
      
;       ; Alloue un nouvel Objet "Properties" de type Structure dynamique qui prend comme valeur l'adresse de la structure "ObjectProperties".
;       ; Allocates a new "Properties" Object of dynamic Structure type which takes as a value the address of the "ObjectProperties" structure.
;       *Object\Properties = AllocateStructure(ObjectProperties)
;       
;       ; Si l'Objet "Properties" n'est pas initialisé.
;       ; If the "Properties" Object is not initialized.
;       If Not *Object\Properties
;         Debug "Error: Unable To allocate Object properties."
;         FreeStructure(*Object) ; Supprime l'Objet car il a pu être créé.   |   Remove the Object as it has been created.
;         ProcedureReturn 0
;       EndIf
      
      ; Affecte un Numéro d'identification à l'Objets en fonction de la variable NextObjectID.i.
      ; Assigns an identification number to the object according to the NextObjectID.i variable.
      *Object\Properties\ID = NextObjectID
      NextObjectID.i + 1
      
      ; Alloue un nouvel Objet "Methods" de type Structure dynamique qui prend comme valeur l'adresse de la structure "ObjectMethods".
      ; Allocates a new "Methods" Object of dynamic Structure type which takes as a value the address of the "ObjectMethods" structure.
   ;   *Object\Methods = AllocateStructure(ObjectMethods)
      
      ; Si l'Objet "Methods" n'est pas initialisé.
      ; If the "Methods" Object is not initialized.
;       If Not *Object\Methods
;         Debug "Error: Unable To allocate Object methods."
;         FreeStructure(*Object\Properties) ; Supprime les propriétés de l'Objet car elles ont pu être créées.   |   Remove the properties of the Object as they have been created.
;         FreeStructure(*Object)            ; Supprime l'Objet car il a pu être créé.   |   Remove the Object as it has been created.
;         ProcedureReturn 0
;       EndIf
      
      ; Methods reçois la structure de l'Objet ou la Table Virtuel est stocké pour réfférencer les Procédures.
      ; Methods receives the structure of the Object where the Virtual Table is stored to refferentiate the Procedures.
;      *Object\Methods = *Object ; Error Here !
      
      ; Retourne un pointeur qui est l'adresse mémoire de la structure "StructureObject".
      ; Returns a pointer which is the memory address of the "StructureObject" structure.
      ProcedureReturn *Object
      
    Else
      ProcedureReturn 0
      
    EndIf
  
  EndProcedure
    
   ; Création de la table virtuel, elle sert à stocker les adresses des procédures (Médhodes).
  ; Create virtual table, It is used to store procedure addresses (Medhodes).
  DataSection
    VirtualTableObject:
    Data.i @Abc123()
    Data.i @Abc246()
    Data.i @AbcXY()
    Data.i @AbcStr()
    Data.i @AbcBox()
    Data.i @DeleteObject()
    Data.i @GetProperties()
  EndDataSection

EndModule

; --------------------------------

; Exemple d'utilisation:
; Example of use:

; ======================================================
; Création d'instance.
; Instance creation.
; ======================================================

UseModule MyObj

; ***************************************************
;   This example is with direct call into Modul - not using the interface
; ***************************************************
*MyObject.StructureObject = NewObject() ; Crée un nouvel Objet.   |   Creates a new Object.

; Si l'Objet existe.   |   If the Object exists.
If *MyObject
  
Debug "Adress Object: " + Str(*MyObject)                       ; Affiche l'adresse mémoire de l'instance de l'Objet.         |   Displays the memory address of the Object instance.
Debug "Adress Object Properties: " + Str(*MyObject\Properties) ; Affiche l'adresse mémoire de la structure des propriétés.   |   Displays the memory address of the properties structure.
;Debug "Adress Object Methods: " + Str(*MyObject\Methods)       ; Affiche l'adresse mémoire de la structure des méthodes.     |   Displays the memory address of the methods structure.
Debug "-----------------------------------"

; ======================================================
; Modification des propriétés.
; Modification of properties.
; ======================================================
*MyObject\Properties\Name = "Object #1"    ; Définit la propriété Name à "Object #1".    |   Sets Name property to "Object #1".
*MyObject\Properties\X = 15                ; Définit la propriété X à la valeur de 15.   |   Sets X property to 15.
*MyObject\Properties\Y = 25                ; Définit la propriété Y à la valeur de 25.   |   Sets Y property to 25.

Debug "Object ID: " + Str(*MyObject\Properties\ID)  ; Affiche la valeur de la propriété ID.                          |   Displays ID property value.
Debug "Object X: " + Str(*MyObject\Properties\X)    ; Affiche la valeur de la propriété X.                           |   Displays X property value.
Debug "Object Y: " + Str(*MyObject\Properties\Y)    ; Affiche la valeur de la propriété Y.                           |   Displays Y property value.
Debug "Object Name: " + *MyObject\Properties\Name   ; Affiche la valeur de la propriété Name sous forme de chaîne.   |   Displays Name property value as a string.
Debug "-----------------------------------"

Debug "Object Fonction Abc123: " + Str(Abc123(*MyObject))      ; Appelle et affiche le résultat de la méthode Abc123().   |    Calls and displays result from Abc123 method().
Debug "Object Fonction Abc246: " + Str(Abc246(*MyObject))       ; Appelle et affiche le résultat de la méthode Abc246().   |    Calls and displays result from Abc246 method().
Debug "Object Fonction AbcXY: " + Str(AbcXY(*MyObject))        ; Appelle et affiche le résultat de la méthode AbcXY().    |    Calls and displays result from AbcXY method().
Debug "Object Fonction AbcStr: " + AbcStr(*MyObject)           ; Appelle et affiche le résultat de la méthode AbcStr().   |    Calls and displays result from AbcStr method().

; Appel de la méthode avec paramètres:   -   Calling a method with parameters:
Debug "Result of AbcBox: " + AbcBox(*MyObject,"Information", "Hello, world!")

DeleteObject(*MyObject) ; Non référencer mais est accepté ! - ; Not referenced but is accepted!

Else
  Debug "Erreur: Objet non initialisés: " + Str(*MyObject)
  
EndIf

Debug ""
Debug "With Interface"
Debug ""
; ***************************************************
;   This example is with using the Obj.-Interface
; ***************************************************

*MyObj.ObjectMethods = NewObject() ; Crée un nouvel Objet.   |   Creates a new Object.


; Si l'Objet existe.   |   If the Object exists.
If *MyObject
  
Debug "Adress Object: " + Str(*MyObj)                       ; Affiche l'adresse mémoire de l'instance de l'Objet.         |   Displays the memory address of the Object instance.
Debug "Adress Object Properties: " + Str(*MyObj\GetProperties()) ; Affiche l'adresse mémoire de la structure des propriétés.   |   Displays the memory address of the properties structure.
;Debug "Adress Object Methods: " + Str(*MyObject\Methods)       ; Affiche l'adresse mémoire de la structure des méthodes.     |   Displays the memory address of the methods structure.
Debug "-----------------------------------"

; ======================================================
; Modification des propriétés.
; Modification of properties.
; ======================================================

*ObjProp.ObjectProperties = *MyObj\GetProperties()

*ObjProp\Name = "Object #1"    ; Définit la propriété Name à "Object #1".    |   Sets Name property to "Object #1".
*ObjProp\X = 15                ; Définit la propriété X à la valeur de 15.   |   Sets X property to 15.
*ObjProp\Y = 25                ; Définit la propriété Y à la valeur de 25.   |   Sets Y property to 25.

Debug "Object ID: " + Str(*ObjProp\ID)  ; Affiche la valeur de la propriété ID.                          |   Displays ID property value.
Debug "Object X: " + Str(*ObjProp\X)    ; Affiche la valeur de la propriété X.                           |   Displays X property value.
Debug "Object Y: " + Str(*ObjProp\Y)    ; Affiche la valeur de la propriété Y.                           |   Displays Y property value.
Debug "Object Name: " + *ObjProp\Name   ; Affiche la valeur de la propriété Name sous forme de chaîne.   |   Displays Name property value as a string.
Debug "-----------------------------------"

Debug "Object Fonction Abc123: " + Str(*MyObj\Abc123())      ; Appelle et affiche le résultat de la méthode Abc123().   |    Calls and displays result from Abc123 method().
Debug "Object Fonction Abc246: " + Str(*MyObj\Abc246())       ; Appelle et affiche le résultat de la méthode Abc246().   |    Calls and displays result from Abc246 method().
Debug "Object Fonction AbcXY: " + Str(*MyObj\AbcXY())        ; Appelle et affiche le résultat de la méthode AbcXY().    |    Calls and displays result from AbcXY method().
Debug "Object Fonction AbcStr: " + *MyObj\AbcStr()           ; Appelle et affiche le résultat de la méthode AbcStr().   |    Calls and displays result from AbcStr method().

; Appel de la méthode avec paramètres:   -   Calling a method with parameters:
Debug "Result of AbcBox: " + *MyObj\AbcBox("Information", "Hello, world!")

DeleteObject(*MyObj) ; Non référencer mais est accepté ! - ; Not referenced but is accepted!

Else
  Debug "Erreur: Objet non initialisés: " + Str(*MyObject)
  
EndIf


Last edited by SMaag on Mon Feb 24, 2025 9:47 am, edited 1 time in total.
ShadowStorm
Enthusiast
Enthusiast
Posts: 303
Joined: Tue Feb 14, 2017 12:07 pm

Re: [Rename] Object-oriented programming simulation for a virtual object manager

Post by ShadowStorm »

Hello Smaag,

Thank you again for your example :) :wink:
However, I don't like it :oops:

Why :?:

I want my object to have fields: Properties, Methods, each displaying their own fields, I want everything to be well partitioned!
I understood what you did, but I don't like it, I don't find it elegant.

And I explained everything about the why and how!

I think my code is good, it works, and I know how I'm going to use it.
With your method, it's going to become even more complicated!

To use it, I would do something like this for example:

Code: Select all

; Création d'une nouvelle Map pour stocker les numero d'objet (clef), valeur = l'emplacement de la structure de l'objet dans la mémoire.
; Create a new Map To store the object number (key), value = the location of the object Structure in memory.
NewMap MapTest.i()

; Initialisation d'un numéro d'objet.  |  Initialization of an object number.
ObjectNumber.i = 1

; Stockage d'un pointeur d'objet dans la Map en utilisant le numéro d'objet comme clé.
; Storing an object pointer in the Map using the object number as key.
MapTest.i(Str(ObjectNumber.i)) = *MyObject

; Récupération du pointeur stocké dans la Map et assignation à une nouvelle variable.
; Retrieval of the pointer stored in the Map and assignment to a new variable.
*Object.StructureObject = MapTest.i(Str(ObjectNumber.i))

; Affichage du nom de l'objet à partir de la structure pointée.
; Display of the object name from the pointed structure.
Debug *Object\Properties\Name
But I still have a few things to look at before that.
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
ShadowStorm
Enthusiast
Enthusiast
Posts: 303
Joined: Tue Feb 14, 2017 12:07 pm

Re: [Rename] Object-oriented programming simulation for a virtual object manager

Post by ShadowStorm »

Hi, there,

Ok well, at the moment I'm encountering a problem that I don't understand at all ! :? :shock:
I'm trying to integrate parent-child management into the code, but I'm having a strange problem even though the code seems to work fine !

The object's AddChild() method isn't referenced, but it's accepted and works as it should.
Why on earth isn't this method referenced, even though I've declared it in the DateSaction and the interface :?: :?: :?:

Code: Select all

; Code créer par Dieppedalle David avec l'aide de l'IA, le 24/02/2025.
; Code created by Dieppedalle David with the help of AI, on 24/02/2025.

; ======================================================
; Structure des propriétés de l'Objet.
; Structure of the Object's properties.
; ======================================================
Structure ObjectProperties

  ID.i     ; Identifiant Unique de l'Objet.  |  Unique Identifier of the Object.
  Name.s   ; Nom de l'Objet.                 |  Name of the Object.
  
  X.i      ; Position Horizontale de l'Objet.  |  Horizontal Position of the Object.
  Y.i      ; Position Verticale de l'Objet.    |  Vertical Position of the Object.
  
  Width.i   ; Largeur de l'Objet.  |  Width of the Object.
  Height.i  ; Hauteur de l'Objet.  |  Height of the Object.
  
  ; Pointeur vers l'Objet Parent (Structure Parent).
  ; Pointer To the Parent Object (Parent Structure).
  *Parent.ObjectProperties  ; Cela représente un Pointeur vers l'Objet Parent.
                      ; Chaque Objet Enfant peut avoir une référence à son Parent, ce qui permet de gérer facilement les relations hiérarchiques.
                      ; 
                      ; This represents a Pointer to the Parent Object.
                      ; Each Child Object can have a reference to its Parent, which allows easy management of hierarchical relationships.
  
  ; Liste des Objets Enfants, où toutes les données des Objets sont stockées dans la liste.
  ; List of Child Objects, where all Object data is stored in the list.
  List *Children.ObjectProperties()  ; Une liste contenant des références (Pointeurs) vers les Objets Enfants.
                                     ; Cela permet de stocker et de manipuler tous les Objets Enfants associés à un Parent spécifique.
                                     ; 
                                     ; A list containing references (Pointers) to Child Objects.
                                     ; This allows storing and manipulating all Child Objects associated with a specific Parent.
  
  ; Ceci est un pointeur qui pointe sur l'adresse d'une étiquette dans la DataSection pour référencer toutes les adresses des procédures (Méthodes) de l'Objet.
  ; This is a pointer that points to the address of a label in the DataSection to reference all the addresses of the Object's procedures (Methods).
  *VirtualTable  ; Table Virtuelle.  |  Virtual Table.
  
EndStructure

; ======================================================
; Interface pour les méthodes de l'Objet.
; Interface for Object methods.
; ======================================================
Interface ObjectMethods ; Les Noms des procédures doivent être identiques à ceux du code, avec les mêmes types et les mêmes paramètres.
                        ; The names of the procedures must be identical to those in the code, with the same types and the same parameters.
  Abc123.i()
  Abc246.i()
  AbcXY.i()
  AbcStr.s()
  AbcBox.s(Title.s, Message.s)
  AddChild.b(*Child.StructureObject) ; Méthode pour ajouter un enfants à l'Objet, *Child.StructureObject est la structure de l'Objet.
                                     ; Method for adding a child to the Object, *Child.StructureObject is the structure of the Object.
  
EndInterface

; ======================================================
; Structure principale de l'Objet.
; Main Object structure.
; ======================================================
Structure StructureObject
  *VirtualTableObject           ; Pointeur (*VirtualTableObject) sans type, qui pointe vers l'adresse des procedures.      |   Pointer (*VirtualTableObject) without type, which points to the address of the procedures.
  *Properties.ObjectProperties  ; Pointeur (*Properties) de type ObjectProperties qui pointe vers les propriétés (état).   |   Pointer (*Properties) of type ObjectProperties that points to properties (state).
  *Methods.ObjectMethods        ; Pointeur (*Methods) de type ObjectMethods qui pointe vers les méthodes (comportement).   |   Pointer (*Methods) of type ObjectMethods that points to methods (behavior).
EndStructure

Global NextObjectID.i = 1

; ======================================================
; Des procédures quelconque. Le premier paramètre d'une fonction '*Object.StructureObject' est automatique ici, PureBasic l'assigne automatiquement quand vous appellerez cette fonction, pas besoin de le remettre.
; Any procedures.            The first parameter of a function '*Object.StructureObject' is automatic here, PureBasic assigns it automatically when you call this function, no need to put it again.
; ======================================================
Procedure.i Abc123(*Object.StructureObject)
  ProcedureReturn 123  ; Retourne la valeur entière 123.   |   Returns the integer value 123.
EndProcedure

Procedure.i Abc246(*Object.StructureObject)
  ProcedureReturn 246  ; Retourne la valeur entière 246.   |   Returns the integer value 246.
EndProcedure

Procedure.i AbcXY(*Object.StructureObject)
  ; Si l'Objet est valide.   |    If the Object is valid.
  If *Object
    ProcedureReturn *Object\Properties\X + *Object\Properties\Y
  Else
    Debug "Error: Object Or properties Not initialized."
    ProcedureReturn 0
  EndIf
EndProcedure

Procedure.s AbcStr(*Object.StructureObject)
  ; Si l'Objet est valide.   |    If the Object is valid.
  If *Object
    ProcedureReturn "X + Y = " + Str(*Object\Properties\X + *Object\Properties\Y)
  Else
    Debug "Error: Object Or properties Not initialized."
    ProcedureReturn ""
  EndIf
EndProcedure

Procedure.s AbcBox(*Object.StructureObject, Title.s, Message.s)
  ; Affiche une boîte de message avec un titre et un message, puis retourne une chaîne de confirmation.
  ; Displays a message box with a title and message, then returns a confirmation string.
  MessageRequester(Title, Message)
  ProcedureReturn "Message displayed !"
EndProcedure

Procedure.b AddChild(*Object.StructureObject, *Child.StructureObject)
  
  ; Vérifie si l'Objet est valide (non-nul).
  ; Checks If Object is valid (non-null).
  If *Object.StructureObject
    
    ; Vérifie si l'Objet enfant est valide (non-nul).
    ; Checks If Child Object is valid (non-null).
    If *Child.StructureObject
      
      *Child\Properties\Parent = *Object.StructureObject
      
      AddElement(*Object.StructureObject\Properties\Children())
      *Object.StructureObject\Properties\Children() = *Child.StructureObject
      
      ProcedureReturn 1
      
    Else
      Debug "Error: Child Object Not initialized: " + Str(*Child)
      ProcedureReturn 0
      
    EndIf
    
  Else
    Debug "Error: Object Not initialized: " + Str(*Object)
    ProcedureReturn 0
    
  EndIf
  
EndProcedure

; Création de la table virtuel, elle sert à stocker les adresses des procédures (Médhodes).
; Create virtual table, It is used to store procedure addresses (Medhodes).
DataSection
  VirtualTableObject:
  Data.i @Abc123()
  Data.i @Abc246()
  Data.i @AbcXY()
  Data.i @AbcStr()
  Data.i @AbcBox()
  Data.i @AddChild()
EndDataSection

; ======================================================
; Création d'un nouvel Objet.
; Creating a new Object.
; ======================================================
Procedure.i NewObject()
  
  ; Déclare un Pointeur (*Object) protégé de type StructureObject, qui prend comme valeur l'adresse de la structure "StructureObject".
  ; Ensuite, alloue un nouvel Objet de type Structure dynamique.
  ; 
  ; Declares a protected Pointer (*Object) of type StructureObject, which takes as a value the address of the "StructureObject" structure.
  ; Then, allocates a new Object of dynamic Structure type.
  Protected *Object.StructureObject = AllocateStructure(StructureObject)
  
  ; Si l'Objet *Object est bien initialisé.
  ; If the Object *Object is properly initialized.
  If *Object.StructureObject
    
    ; Définit la table virtuelle de l'objet pour référencer les méthodes de celui-ci.
    ; Defines the virtual table of the object to reference its methods.
    *Object\VirtualTableObject = ?VirtualTableObject
    
    ; Alloue un nouvel Objet "Properties" de type Structure dynamique qui prend comme valeur l'adresse de la structure "ObjectProperties".
    ; Allocates a new "Properties" Object of dynamic Structure type which takes as a value the address of the "ObjectProperties" structure.
    *Object\Properties = AllocateStructure(ObjectProperties)
    
    ; Si l'Objet "Properties" n'est pas initialisé.
    ; If the "Properties" Object is not initialized.
    If Not *Object\Properties
      Debug "Error: Unable To allocate Object properties."
      FreeStructure(*Object) ; Supprime l'Objet car il a pu être créé.   |   Remove the Object as it has been created.
      ProcedureReturn 0
    EndIf
    
    ; Affecte un Numéro d'identification à l'Objets en fonction de la variable NextObjectID.i.
    ; Assigns an identification number to the object according to the NextObjectID.i variable.
    *Object\Properties\ID = NextObjectID.i
    NextObjectID.i + 1
    
    ; Alloue un nouvel Objet "Methods" de type Structure dynamique qui prend comme valeur l'adresse de la structure "ObjectMethods".
    ; Allocates a new "Methods" Object of dynamic Structure type which takes as a value the address of the "ObjectMethods" structure.
    *Object\Methods = AllocateStructure(ObjectMethods)
    
    ; Si l'Objet "Methods" n'est pas initialisé.
    ; If the "Methods" Object is not initialized.
    If Not *Object\Methods
      Debug "Error: Unable To allocate Object methods."
      FreeStructure(*Object\Properties) ; Supprime les propriétés de l'Objet car elles ont pu être créées.   |   Remove the properties of the Object as they have been created.
      FreeStructure(*Object)            ; Supprime l'Objet car il a pu être créé.   |   Remove the Object as it has been created.
      ProcedureReturn 0
    EndIf
    
    ; Methods reçois la structure de l'Objet ou la Table Virtuel est stocké pour réfférencer les Procédures.
    ; Methods receives the structure of the Object where the Virtual Table is stored to refferentiate the Procedures.
    *Object\Methods = *Object
    
    ; Retourne un pointeur qui est l'adresse mémoire de la structure "StructureObject".
    ; Returns a pointer which is the memory address of the "StructureObject" structure.
    ProcedureReturn *Object
    
  Else
    ProcedureReturn 0
    
  EndIf
  
EndProcedure

; ======================================================
; Supprime un objet et libère toute sa mémoire allouée.
; Delete an Object and releases all its allocated memory.
; ======================================================
Procedure.b DeleteObject(*Object.StructureObject)
  
  ; Vérifie si l'objet est valide (non-nul).
  ; Checks If Object is valid (non-null).
  If *Object.StructureObject
    
    ; Si l'Objet a une structure Properties initialisée.
    ; If the Object has an initialized Properties structure.
    If *Object\Properties 
      
      ; Libère les propriétés.
      ; Frees properties.
      ClearStructure(*Object\Properties, ObjectProperties)
      FreeStructure(*Object\Properties)
      
      ; Si l'Objet a une structure Methods initialisée.
      ; If the Object has an initialized Methods structure.
      If *Object\Methods 
        
        ; Libère les méthodes   |   Frees methods.
        ClearStructure(*Object\Methods, ObjectMethods)
        FreeStructure(*Object\Methods)
        
        ProcedureReturn 1
        
      EndIf
      
    EndIf
    
    ProcedureReturn 0
    
  Else
    Debug "Error: Object Not initialized: " + Str(*Object)
    ProcedureReturn 0
    
  EndIf
  
EndProcedure


; 
;   ; Vérifie si l'objet est valide (non-nul).
;   ; Checks If Object is valid (non-null).
;   If *Object.StructureObject
;   
;   Else
;     Debug "Error: Object Not initialized: " + Str(*Object)
;     ProcedureReturn 0
;     
;   EndIf
;   
  
; --------------------------------

; Exemple d'utilisation:
; Example of use:

; ======================================================
; Création d'instance.
; Instance creation.
; ======================================================
*MyObjectRoot.StructureObject = NewObject() ; Crée un nouvel Objet.   |   Creates a new Object.

; Si l'Objet existe.   |   If the Object exists.
If *MyObjectRoot
  
  Debug "Adress Object: " + Str(*MyObjectRoot)                       ; Affiche l'adresse mémoire de l'instance de l'Objet.         |   Displays the memory address of the Object instance.
  Debug "Adress Object Properties: " + Str(*MyObjectRoot\Properties) ; Affiche l'adresse mémoire de la structure des propriétés.   |   Displays the memory address of the properties structure.
  Debug "Adress Object Methods: " + Str(*MyObjectRoot\Methods)       ; Affiche l'adresse mémoire de la structure des méthodes.     |   Displays the memory address of the methods structure.
  Debug "-----------------------------------"
  
  ; ======================================================
  ; Modification des propriétés.
  ; Modification of properties.
  ; ======================================================
  *MyObjectRoot\Properties\Name = "Object #1"    ; Définit la propriété Name à "Object #1".    |   Sets Name property to "Object #1".
  *MyObjectRoot\Properties\X = 15                ; Définit la propriété X à la valeur de 15.   |   Sets X property to 15.
  *MyObjectRoot\Properties\Y = 25                ; Définit la propriété Y à la valeur de 25.   |   Sets Y property to 25.
  
  Debug "Object ID: " + Str(*MyObjectRoot\Properties\ID)  ; Affiche la valeur de la propriété ID.                          |   Displays ID property value.
  Debug "Object X: " + Str(*MyObjectRoot\Properties\X)    ; Affiche la valeur de la propriété X.                           |   Displays X property value.
  Debug "Object Y: " + Str(*MyObjectRoot\Properties\Y)    ; Affiche la valeur de la propriété Y.                           |   Displays Y property value.
  Debug "Object Name: " + *MyObjectRoot\Properties\Name   ; Affiche la valeur de la propriété Name sous forme de chaîne.   |   Displays Name property value as a string.
  Debug "-----------------------------------"
  
  Debug "Object Fonction Abc123: " + Str(*MyObjectRoot\Methods\Abc123())       ; Appelle et affiche le résultat de la méthode Abc123().   |    Calls and displays result from Abc123 method().
  Debug "Object Fonction Abc246: " + Str(*MyObjectRoot\Methods\Abc246())       ; Appelle et affiche le résultat de la méthode Abc246().   |    Calls and displays result from Abc246 method().
  Debug "Object Fonction AbcXY: " + Str(*MyObjectRoot\Methods\AbcXY())         ; Appelle et affiche le résultat de la méthode AbcXY().    |    Calls and displays result from AbcXY method().
  Debug "Object Fonction AbcStr: " + *MyObjectRoot\Methods\AbcStr()            ; Appelle et affiche le résultat de la méthode AbcStr().   |    Calls and displays result from AbcStr method().
  
  ; Appel de la méthode avec paramètres:   -   Calling a method with parameters:
  Debug "Result of AbcBox: " + *MyObjectRoot\Methods\AbcBox("Information", "Hello, world!")
  
;   ; Pour supprimer un objet, suivez scrupuleusement cette méthode !
;   ; To delete an object, follow this method carefully !
;   If DeleteObject(*MyObjectRoot)
;     *MyObjectRoot = #NUL
;     Debug "Object deleted !" 
;   EndIf
  
Else
  Debug "Error: Uninitialized objects: " + Str(*MyObjectRoot)
  
EndIf

Debug "-----------------------------------"
Debug "-----------------------------------"

*MyObjectChild1.StructureObject = NewObject() ; Crée un nouvel Objet.   |   Creates a new Object.

; Si l'Objet existe.   |   If the Object exists.
If *MyObjectChild1
  
  Debug "Adress Object: " + Str(*MyObjectChild1)                       ; Affiche l'adresse mémoire de l'instance de l'Objet.         |   Displays the memory address of the Object instance.
  Debug "Adress Object Properties: " + Str(*MyObjectChild1\Properties) ; Affiche l'adresse mémoire de la structure des propriétés.   |   Displays the memory address of the properties structure.
  Debug "Adress Object Methods: " + Str(*MyObjectChild1\Methods)       ; Affiche l'adresse mémoire de la structure des méthodes.     |   Displays the memory address of the methods structure.
  Debug "-----------------------------------"
  
  ; ======================================================
  ; Modification des propriétés.
  ; Modification of properties.
  ; ======================================================
  *MyObjectChild1\Properties\Name = "Object #1"    ; Définit la propriété Name à "Object #1".    |   Sets Name property to "Object #1".
  *MyObjectChild1\Properties\X = 15                ; Définit la propriété X à la valeur de 15.   |   Sets X property to 15.
  *MyObjectChild1\Properties\Y = 25                ; Définit la propriété Y à la valeur de 25.   |   Sets Y property to 25.
  
  Debug "Object ID: " + Str(*MyObjectChild1\Properties\ID)  ; Affiche la valeur de la propriété ID.                          |   Displays ID property value.
  Debug "Object X: " + Str(*MyObjectChild1\Properties\X)    ; Affiche la valeur de la propriété X.                           |   Displays X property value.
  Debug "Object Y: " + Str(*MyObjectChild1\Properties\Y)    ; Affiche la valeur de la propriété Y.                           |   Displays Y property value.
  Debug "Object Name: " + *MyObjectChild1\Properties\Name   ; Affiche la valeur de la propriété Name sous forme de chaîne.   |   Displays Name property value as a string.
  Debug "-----------------------------------"
  
  Debug "Object Fonction Abc123: " + Str(*MyObjectChild1\Methods\Abc123())       ; Appelle et affiche le résultat de la méthode Abc123().   |    Calls and displays result from Abc123 method().
  Debug "Object Fonction Abc246: " + Str(*MyObjectChild1\Methods\Abc246())       ; Appelle et affiche le résultat de la méthode Abc246().   |    Calls and displays result from Abc246 method().
  Debug "Object Fonction AbcXY: " + Str(*MyObjectChild1\Methods\AbcXY())         ; Appelle et affiche le résultat de la méthode AbcXY().    |    Calls and displays result from AbcXY method().
  Debug "Object Fonction AbcStr: " + *MyObjectChild1\Methods\AbcStr()            ; Appelle et affiche le résultat de la méthode AbcStr().   |    Calls and displays result from AbcStr method().
  
  ; Appel de la méthode avec paramètres:   -   Calling a method with parameters:
  Debug "Result of AbcBox: " + *MyObjectChild1\Methods\AbcBox("Information", "Hello, world!")
  
;   ; Pour supprimer un objet, suivez scrupuleusement cette méthode !
;   ; To delete an object, follow this method carefully !
;   If DeleteObject(*MyObjectChild1)
;     *MyObjectChild1 = #NUL
;     Debug "Object deleted !" 
;   EndIf
  
Else
  Debug "Error: Uninitialized objects: " + Str(*MyObjectChild1)
  
EndIf

If *MyObjectRoot\Methods\AddChild(*MyObjectChild1) ; Non réfférencé dans les méthodes de l'objet mais accepté.  |  Not re-referenced in the object's methods but accepted.
  Debug "Info: Add Child objects: " + Str(*MyObjectChild1) + " as Parent Object: " + Str(*MyObjectChild1\Properties\Parent)
EndIf

Debug "-----------------------------------" 
Debug "-----------------------------------"

*MyObjectChild2.StructureObject = NewObject() ; Crée un nouvel Objet.   |   Creates a new Object.

; Si l'Objet existe.   |   If the Object exists.
If *MyObjectChild2
  
  Debug "Adress Object: " + Str(*MyObjectChild2)                       ; Affiche l'adresse mémoire de l'instance de l'Objet.         |   Displays the memory address of the Object instance.
  Debug "Adress Object Properties: " + Str(*MyObjectChild2\Properties) ; Affiche l'adresse mémoire de la structure des propriétés.   |   Displays the memory address of the properties structure.
  Debug "Adress Object Methods: " + Str(*MyObjectChild2\Methods)       ; Affiche l'adresse mémoire de la structure des méthodes.     |   Displays the memory address of the methods structure.
  Debug "-----------------------------------"
  
  ; ======================================================
  ; Modification des propriétés.
  ; Modification of properties.
  ; ======================================================
  *MyObjectChild2\Properties\Name = "Object #1"    ; Définit la propriété Name à "Object #1".    |   Sets Name property to "Object #1".
  *MyObjectChild2\Properties\X = 15                ; Définit la propriété X à la valeur de 15.   |   Sets X property to 15.
  *MyObjectChild2\Properties\Y = 25                ; Définit la propriété Y à la valeur de 25.   |   Sets Y property to 25.
  
  Debug "Object ID: " + Str(*MyObjectChild2\Properties\ID)  ; Affiche la valeur de la propriété ID.                          |   Displays ID property value.
  Debug "Object X: " + Str(*MyObjectChild2\Properties\X)    ; Affiche la valeur de la propriété X.                           |   Displays X property value.
  Debug "Object Y: " + Str(*MyObjectChild2\Properties\Y)    ; Affiche la valeur de la propriété Y.                           |   Displays Y property value.
  Debug "Object Name: " + *MyObjectChild2\Properties\Name   ; Affiche la valeur de la propriété Name sous forme de chaîne.   |   Displays Name property value as a string.
  Debug "-----------------------------------"
  
  Debug "Object Fonction Abc123: " + Str(*MyObjectChild2\Methods\Abc123())       ; Appelle et affiche le résultat de la méthode Abc123().   |    Calls and displays result from Abc123 method().
  Debug "Object Fonction Abc246: " + Str(*MyObjectChild2\Methods\Abc246())       ; Appelle et affiche le résultat de la méthode Abc246().   |    Calls and displays result from Abc246 method().
  Debug "Object Fonction AbcXY: " + Str(*MyObjectChild2\Methods\AbcXY())         ; Appelle et affiche le résultat de la méthode AbcXY().    |    Calls and displays result from AbcXY method().
  Debug "Object Fonction AbcStr: " + *MyObjectChild2\Methods\AbcStr()            ; Appelle et affiche le résultat de la méthode AbcStr().   |    Calls and displays result from AbcStr method().
  
  ; Appel de la méthode avec paramètres:   -   Calling a method with parameters:
  Debug "Result of AbcBox: " + *MyObjectChild2\Methods\AbcBox("Information", "Hello, world!")
  
;   ; Pour supprimer un objet, suivez scrupuleusement cette méthode !
;   ; To delete an object, follow this method carefully !
;   If DeleteObject(*MyObjectChild2)
;     *MyObjectChild2 = #NUL
;     Debug "Object deleted !" 
;   EndIf
  
Else
  Debug "Error: Uninitialized objects: " + Str(*MyObjectChild2)
  
EndIf

If *MyObjectChild1\Methods\AddChild(*MyObjectChild2) ; Non réfférencé dans les méthodes de l'objet mais accepté.  |  Not re-referenced in the object's methods but accepted.
  Debug "Info: Add Child objects: " + Str(*MyObjectChild2) + " as Parent Object: " + Str(*MyObjectChild2\Properties\Parent)
EndIf

; Pour supprimer un objet, suivez scrupuleusement cette méthode !
; To delete an object, follow this method carefully !
If DeleteObject(*MyObjectRoot)
  *MyObjectRoot = #NUL
  Debug "Object deleted !" 
EndIf
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
ShadowStorm
Enthusiast
Enthusiast
Posts: 303
Joined: Tue Feb 14, 2017 12:07 pm

Re: [Rename] Object-oriented programming simulation for a virtual object manager

Post by ShadowStorm »

I've tried it this way, it might be better but it doesn't change anything in the end.

Code: Select all

; Code créer par Dieppedalle David avec l'aide de l'IA, le 24/02/2025.
; Code created by Dieppedalle David with the help of AI, on 24/02/2025.

; ======================================================
; Structure des propriétés de l'Objet.
; Structure of the Object's properties.
; ======================================================
Structure ObjectProperties

  ID.i     ; Identifiant Unique de l'Objet.  |  Unique Identifier of the Object.
  Name.s   ; Nom de l'Objet.                 |  Name of the Object.
  
  X.i      ; Position Horizontale de l'Objet.  |  Horizontal Position of the Object.
  Y.i      ; Position Verticale de l'Objet.    |  Vertical Position of the Object.
  
  Width.i   ; Largeur de l'Objet.  |  Width of the Object.
  Height.i  ; Hauteur de l'Objet.  |  Height of the Object.
  
EndStructure

; ======================================================
; Interface pour les méthodes de l'Objet.
; Interface for Object methods.
; ======================================================
Interface ObjectMethods ; Les Noms des procédures doivent être identiques à ceux du code, avec les mêmes types et les mêmes paramètres.
                        ; The names of the procedures must be identical to those in the code, with the same types and the same parameters.
  Abc123.i()
  Abc246.i()
  AbcXY.i()
  AbcStr.s()
  AbcBox.s(Title.s, Message.s)
  AddChild.b(*Child.StructureObject) ; Méthode pour ajouter un enfants à l'Objet, *Child.StructureObject est la structure de l'Objet.
                                     ; Method for adding a child to the Object, *Child.StructureObject is the structure of the Object.
  
EndInterface

; ======================================================
; Structure principale de l'Objet.
; Main Object structure.
; ======================================================
Structure StructureObject
  
  *VirtualTableObject           ; Pointeur (*VirtualTableObject) sans type, qui pointe vers l'adresse des procedures.      |   Pointer (*VirtualTableObject) without type, which points to the address of the procedures.
  
  *Properties.ObjectProperties  ; Pointeur (*Properties) de type ObjectProperties qui pointe vers les propriétés (état).   |   Pointer (*Properties) of type ObjectProperties that points to properties (state).
  *Methods.ObjectMethods        ; Pointeur (*Methods) de type ObjectMethods qui pointe vers les méthodes (comportement).   |   Pointer (*Methods) of type ObjectMethods that points to methods (behavior).
  
  ; Pointeur vers l'Objet Parent (Structure Parent).
  ; Pointer To the Parent Object (Parent Structure).
  *Parent.StructureObject  ; Cela représente un Pointeur vers l'Objet Parent.
                      ; Chaque Objet Enfant peut avoir une référence à son Parent, ce qui permet de gérer facilement les relations hiérarchiques.
                      ; 
                      ; This represents a Pointer to the Parent Object.
                      ; Each Child Object can have a reference to its Parent, which allows easy management of hierarchical relationships.
  
  ; Liste des Objets Enfants, où toutes les données des Objets sont stockées dans la liste.
  ; List of Child Objects, where all Object data is stored in the list.
  List *Children.StructureObject()  ; Une liste contenant des références (Pointeurs) vers les Objets Enfants.
                                     ; Cela permet de stocker et de manipuler tous les Objets Enfants associés à un Parent spécifique.
                                     ; 
                                     ; A list containing references (Pointers) to Child Objects.
                                     ; This allows storing and manipulating all Child Objects associated with a specific Parent.
  
EndStructure


Global NextObjectID.i = 1

; ======================================================
; Des procédures quelconque. Le premier paramètre d'une fonction '*Object.StructureObject' est automatique ici, PureBasic l'assigne automatiquement quand vous appellerez cette fonction, pas besoin de le remettre.
; Any procedures.            The first parameter of a function '*Object.StructureObject' is automatic here, PureBasic assigns it automatically when you call this function, no need to put it again.
; ======================================================
Procedure.i Abc123(*Object.StructureObject)
  ProcedureReturn 123  ; Retourne la valeur entière 123.   |   Returns the integer value 123.
EndProcedure

Procedure.i Abc246(*Object.StructureObject)
  ProcedureReturn 246  ; Retourne la valeur entière 246.   |   Returns the integer value 246.
EndProcedure

Procedure.i AbcXY(*Object.StructureObject)
  ; Si l'Objet est valide.   |    If the Object is valid.
  If *Object
    ProcedureReturn *Object\Properties\X + *Object\Properties\Y
  Else
    Debug "Error: Object Or properties Not initialized."
    ProcedureReturn 0
  EndIf
EndProcedure

Procedure.s AbcStr(*Object.StructureObject)
  ; Si l'Objet est valide.   |    If the Object is valid.
  If *Object
    ProcedureReturn "X + Y = " + Str(*Object\Properties\X + *Object\Properties\Y)
  Else
    Debug "Error: Object Or properties Not initialized."
    ProcedureReturn ""
  EndIf
EndProcedure

Procedure.s AbcBox(*Object.StructureObject, Title.s, Message.s)
  ; Affiche une boîte de message avec un titre et un message, puis retourne une chaîne de confirmation.
  ; Displays a message box with a title and message, then returns a confirmation string.
  MessageRequester(Title, Message)
  ProcedureReturn "Message displayed !"
EndProcedure

Procedure.b AddChild(*Object.StructureObject, *Child.StructureObject)
  
  ; Vérifie si l'Objet est valide (non-nul).
  ; Checks If Object is valid (non-null).
  If *Object.StructureObject
    
    ; Vérifie si l'Objet enfant est valide (non-nul).
    ; Checks If Child Object is valid (non-null).
    If *Child.StructureObject
      
      *Child\Parent = *Object
      
      AddElement(*Object.StructureObject\Children())
      *Object.StructureObject\Children() = *Child.StructureObject
      
      ProcedureReturn 1
      
    Else
      Debug "Error: Child Object Not initialized: " + Str(*Child)
      ProcedureReturn 0
      
    EndIf
    
  Else
    Debug "Error: Object Not initialized: " + Str(*Object)
    ProcedureReturn 0
    
  EndIf
  
EndProcedure

; Création de la table virtuel, elle sert à stocker les adresses des procédures (Médhodes).
; Create virtual table, It is used to store procedure addresses (Medhodes).
DataSection
  VirtualTableObject:
  Data.i @Abc123()
  Data.i @Abc246()
  Data.i @AbcXY()
  Data.i @AbcStr()
  Data.i @AbcBox()
  Data.i @AddChild()
EndDataSection

; ======================================================
; Création d'un nouvel Objet.
; Creating a new Object.
; ======================================================
Procedure.i NewObject()
  
  ; Déclare un Pointeur (*Object) protégé de type StructureObject, qui prend comme valeur l'adresse de la structure "StructureObject".
  ; Ensuite, alloue un nouvel Objet de type Structure dynamique.
  ; 
  ; Declares a protected Pointer (*Object) of type StructureObject, which takes as a value the address of the "StructureObject" structure.
  ; Then, allocates a new Object of dynamic Structure type.
  Protected *Object.StructureObject = AllocateStructure(StructureObject)
  
  ; Si l'Objet *Object est bien initialisé.
  ; If the Object *Object is properly initialized.
  If *Object.StructureObject
    
    ; Définit la table virtuelle de l'objet pour référencer les méthodes de celui-ci.
    ; Defines the virtual table of the object to reference its methods.
    *Object\VirtualTableObject = ?VirtualTableObject
    
    ; Alloue un nouvel Objet "Properties" de type Structure dynamique qui prend comme valeur l'adresse de la structure "ObjectProperties".
    ; Allocates a new "Properties" Object of dynamic Structure type which takes as a value the address of the "ObjectProperties" structure.
    *Object\Properties = AllocateStructure(ObjectProperties)
    
    ; Si l'Objet "Properties" n'est pas initialisé.
    ; If the "Properties" Object is not initialized.
    If Not *Object\Properties
      Debug "Error: Unable To allocate Object properties."
      FreeStructure(*Object) ; Supprime l'Objet car il a pu être créé.   |   Remove the Object as it has been created.
      ProcedureReturn 0
    EndIf
    
    ; Affecte un Numéro d'identification à l'Objets en fonction de la variable NextObjectID.i.
    ; Assigns an identification number to the object according to the NextObjectID.i variable.
    *Object\Properties\ID = NextObjectID.i
    NextObjectID.i + 1
    
    ; Alloue un nouvel Objet "Methods" de type Structure dynamique qui prend comme valeur l'adresse de la structure "ObjectMethods".
    ; Allocates a new "Methods" Object of dynamic Structure type which takes as a value the address of the "ObjectMethods" structure.
    *Object\Methods = AllocateStructure(ObjectMethods)
    
    ; Si l'Objet "Methods" n'est pas initialisé.
    ; If the "Methods" Object is not initialized.
    If Not *Object\Methods
      Debug "Error: Unable To allocate Object methods."
      FreeStructure(*Object\Properties) ; Supprime les propriétés de l'Objet car elles ont pu être créées.   |   Remove the properties of the Object as they have been created.
      FreeStructure(*Object)            ; Supprime l'Objet car il a pu être créé.   |   Remove the Object as it has been created.
      ProcedureReturn 0
    EndIf
    
    ; Methods reçois la structure de l'Objet ou la Table Virtuel est stocké pour réfférencer les Procédures.
    ; Methods receives the structure of the Object where the Virtual Table is stored to refferentiate the Procedures.
    *Object\Methods = *Object
    
    ; Retourne un pointeur qui est l'adresse mémoire de la structure "StructureObject".
    ; Returns a pointer which is the memory address of the "StructureObject" structure.
    ProcedureReturn *Object
    
  Else
    ProcedureReturn 0
    
  EndIf
  
EndProcedure

; ======================================================
; Supprime un objet et libère toute sa mémoire allouée.
; Delete an Object and releases all its allocated memory.
; ======================================================
Procedure.b DeleteObject(*Object.StructureObject)
  
  ; Vérifie si l'objet est valide (non-nul).
  ; Checks If Object is valid (non-null).
  If *Object.StructureObject
    
    ; Si l'Objet a une structure Properties initialisée.
    ; If the Object has an initialized Properties structure.
    If *Object\Properties 
      
      ; Libère les propriétés.
      ; Frees properties.
      ClearStructure(*Object\Properties, ObjectProperties)
      FreeStructure(*Object\Properties)
      
      ; Si l'Objet a une structure Methods initialisée.
      ; If the Object has an initialized Methods structure.
      If *Object\Methods 
        
        ; Libère les méthodes   |   Frees methods.
        ClearStructure(*Object\Methods, ObjectMethods)
        FreeStructure(*Object\Methods)
        
        ProcedureReturn 1
        
      EndIf
      
    EndIf
    
    ProcedureReturn 0
    
  Else
    Debug "Error: Object Not initialized: " + Str(*Object)
    ProcedureReturn 0
    
  EndIf
  
EndProcedure


; 
;   ; Vérifie si l'objet est valide (non-nul).
;   ; Checks If Object is valid (non-null).
;   If *Object.StructureObject
;   
;   Else
;     Debug "Error: Object Not initialized: " + Str(*Object)
;     ProcedureReturn 0
;     
;   EndIf
;   
  
; --------------------------------

; Exemple d'utilisation:
; Example of use:

; ======================================================
; Création d'instance.
; Instance creation.
; ======================================================
*MyObjectRoot.StructureObject = NewObject() ; Crée un nouvel Objet.   |   Creates a new Object.

; Si l'Objet existe.   |   If the Object exists.
If *MyObjectRoot
  
  Debug "Adress Object: " + Str(*MyObjectRoot)                       ; Affiche l'adresse mémoire de l'instance de l'Objet.         |   Displays the memory address of the Object instance.
  Debug "Adress Object Properties: " + Str(*MyObjectRoot\Properties) ; Affiche l'adresse mémoire de la structure des propriétés.   |   Displays the memory address of the properties structure.
  Debug "Adress Object Methods: " + Str(*MyObjectRoot\Methods)       ; Affiche l'adresse mémoire de la structure des méthodes.     |   Displays the memory address of the methods structure.
  Debug "-----------------------------------"
  
  ; ======================================================
  ; Modification des propriétés.
  ; Modification of properties.
  ; ======================================================
  *MyObjectRoot\Properties\Name = "Object #1"    ; Définit la propriété Name à "Object #1".    |   Sets Name property to "Object #1".
  *MyObjectRoot\Properties\X = 15                ; Définit la propriété X à la valeur de 15.   |   Sets X property to 15.
  *MyObjectRoot\Properties\Y = 25                ; Définit la propriété Y à la valeur de 25.   |   Sets Y property to 25.
  
  Debug "Object ID: " + Str(*MyObjectRoot\Properties\ID)  ; Affiche la valeur de la propriété ID.                          |   Displays ID property value.
  Debug "Object X: " + Str(*MyObjectRoot\Properties\X)    ; Affiche la valeur de la propriété X.                           |   Displays X property value.
  Debug "Object Y: " + Str(*MyObjectRoot\Properties\Y)    ; Affiche la valeur de la propriété Y.                           |   Displays Y property value.
  Debug "Object Name: " + *MyObjectRoot\Properties\Name   ; Affiche la valeur de la propriété Name sous forme de chaîne.   |   Displays Name property value as a string.
  Debug "-----------------------------------"
  
  Debug "Object Fonction Abc123: " + Str(*MyObjectRoot\Methods\Abc123())       ; Appelle et affiche le résultat de la méthode Abc123().   |    Calls and displays result from Abc123 method().
  Debug "Object Fonction Abc246: " + Str(*MyObjectRoot\Methods\Abc246())       ; Appelle et affiche le résultat de la méthode Abc246().   |    Calls and displays result from Abc246 method().
  Debug "Object Fonction AbcXY: " + Str(*MyObjectRoot\Methods\AbcXY())         ; Appelle et affiche le résultat de la méthode AbcXY().    |    Calls and displays result from AbcXY method().
  Debug "Object Fonction AbcStr: " + *MyObjectRoot\Methods\AbcStr()            ; Appelle et affiche le résultat de la méthode AbcStr().   |    Calls and displays result from AbcStr method().
  
  ; Appel de la méthode avec paramètres:   -   Calling a method with parameters:
  Debug "Result of AbcBox: " + *MyObjectRoot\Methods\AbcBox("Information", "Hello, world!")
  
;   ; Pour supprimer un objet, suivez scrupuleusement cette méthode !
;   ; To delete an object, follow this method carefully !
;   If DeleteObject(*MyObjectRoot)
;     *MyObjectRoot = #NUL
;     Debug "Object deleted !" 
;   EndIf
  
Else
  Debug "Error: Uninitialized objects: " + Str(*MyObjectRoot)
  
EndIf

Debug "-----------------------------------"
Debug "-----------------------------------"

*MyObjectChild1.StructureObject = NewObject() ; Crée un nouvel Objet.   |   Creates a new Object.

; Si l'Objet existe.   |   If the Object exists.
If *MyObjectChild1
  
  Debug "Adress Object: " + Str(*MyObjectChild1)                       ; Affiche l'adresse mémoire de l'instance de l'Objet.         |   Displays the memory address of the Object instance.
  Debug "Adress Object Properties: " + Str(*MyObjectChild1\Properties) ; Affiche l'adresse mémoire de la structure des propriétés.   |   Displays the memory address of the properties structure.
  Debug "Adress Object Methods: " + Str(*MyObjectChild1\Methods)       ; Affiche l'adresse mémoire de la structure des méthodes.     |   Displays the memory address of the methods structure.
  Debug "-----------------------------------"
  
  ; ======================================================
  ; Modification des propriétés.
  ; Modification of properties.
  ; ======================================================
  *MyObjectChild1\Properties\Name = "Object #1"    ; Définit la propriété Name à "Object #1".    |   Sets Name property to "Object #1".
  *MyObjectChild1\Properties\X = 15                ; Définit la propriété X à la valeur de 15.   |   Sets X property to 15.
  *MyObjectChild1\Properties\Y = 25                ; Définit la propriété Y à la valeur de 25.   |   Sets Y property to 25.
  
  Debug "Object ID: " + Str(*MyObjectChild1\Properties\ID)  ; Affiche la valeur de la propriété ID.                          |   Displays ID property value.
  Debug "Object X: " + Str(*MyObjectChild1\Properties\X)    ; Affiche la valeur de la propriété X.                           |   Displays X property value.
  Debug "Object Y: " + Str(*MyObjectChild1\Properties\Y)    ; Affiche la valeur de la propriété Y.                           |   Displays Y property value.
  Debug "Object Name: " + *MyObjectChild1\Properties\Name   ; Affiche la valeur de la propriété Name sous forme de chaîne.   |   Displays Name property value as a string.
  Debug "-----------------------------------"
  
  Debug "Object Fonction Abc123: " + Str(*MyObjectChild1\Methods\Abc123())       ; Appelle et affiche le résultat de la méthode Abc123().   |    Calls and displays result from Abc123 method().
  Debug "Object Fonction Abc246: " + Str(*MyObjectChild1\Methods\Abc246())       ; Appelle et affiche le résultat de la méthode Abc246().   |    Calls and displays result from Abc246 method().
  Debug "Object Fonction AbcXY: " + Str(*MyObjectChild1\Methods\AbcXY())         ; Appelle et affiche le résultat de la méthode AbcXY().    |    Calls and displays result from AbcXY method().
  Debug "Object Fonction AbcStr: " + *MyObjectChild1\Methods\AbcStr()            ; Appelle et affiche le résultat de la méthode AbcStr().   |    Calls and displays result from AbcStr method().
  
  ; Appel de la méthode avec paramètres:   -   Calling a method with parameters:
  Debug "Result of AbcBox: " + *MyObjectChild1\Methods\AbcBox("Information", "Hello, world!")
  
;   ; Pour supprimer un objet, suivez scrupuleusement cette méthode !
;   ; To delete an object, follow this method carefully !
;   If DeleteObject(*MyObjectChild1)
;     *MyObjectChild1 = #NUL
;     Debug "Object deleted !" 
;   EndIf
  
Else
  Debug "Error: Uninitialized objects: " + Str(*MyObjectChild1)
  
EndIf

If *MyObjectRoot\Methods\AddChild(*MyObjectChild1) ; Non réfférencé dans les méthodes de l'objet mais accepté.  |  Not re-referenced in the object's methods but accepted.
  Debug "Info: Add Child objects: " + Str(*MyObjectChild1) + " as Parent Object: " + Str(*MyObjectChild1\Parent)
EndIf

Debug "-----------------------------------" 
Debug "-----------------------------------"

*MyObjectChild2.StructureObject = NewObject() ; Crée un nouvel Objet.   |   Creates a new Object.

; Si l'Objet existe.   |   If the Object exists.
If *MyObjectChild2
  
  Debug "Adress Object: " + Str(*MyObjectChild2)                       ; Affiche l'adresse mémoire de l'instance de l'Objet.         |   Displays the memory address of the Object instance.
  Debug "Adress Object Properties: " + Str(*MyObjectChild2\Properties) ; Affiche l'adresse mémoire de la structure des propriétés.   |   Displays the memory address of the properties structure.
  Debug "Adress Object Methods: " + Str(*MyObjectChild2\Methods)       ; Affiche l'adresse mémoire de la structure des méthodes.     |   Displays the memory address of the methods structure.
  Debug "-----------------------------------"
  
  ; ======================================================
  ; Modification des propriétés.
  ; Modification of properties.
  ; ======================================================
  *MyObjectChild2\Properties\Name = "Object #1"    ; Définit la propriété Name à "Object #1".    |   Sets Name property to "Object #1".
  *MyObjectChild2\Properties\X = 15                ; Définit la propriété X à la valeur de 15.   |   Sets X property to 15.
  *MyObjectChild2\Properties\Y = 25                ; Définit la propriété Y à la valeur de 25.   |   Sets Y property to 25.
  
  Debug "Object ID: " + Str(*MyObjectChild2\Properties\ID)  ; Affiche la valeur de la propriété ID.                          |   Displays ID property value.
  Debug "Object X: " + Str(*MyObjectChild2\Properties\X)    ; Affiche la valeur de la propriété X.                           |   Displays X property value.
  Debug "Object Y: " + Str(*MyObjectChild2\Properties\Y)    ; Affiche la valeur de la propriété Y.                           |   Displays Y property value.
  Debug "Object Name: " + *MyObjectChild2\Properties\Name   ; Affiche la valeur de la propriété Name sous forme de chaîne.   |   Displays Name property value as a string.
  Debug "-----------------------------------"
  
  Debug "Object Fonction Abc123: " + Str(*MyObjectChild2\Methods\Abc123())       ; Appelle et affiche le résultat de la méthode Abc123().   |    Calls and displays result from Abc123 method().
  Debug "Object Fonction Abc246: " + Str(*MyObjectChild2\Methods\Abc246())       ; Appelle et affiche le résultat de la méthode Abc246().   |    Calls and displays result from Abc246 method().
  Debug "Object Fonction AbcXY: " + Str(*MyObjectChild2\Methods\AbcXY())         ; Appelle et affiche le résultat de la méthode AbcXY().    |    Calls and displays result from AbcXY method().
  Debug "Object Fonction AbcStr: " + *MyObjectChild2\Methods\AbcStr()            ; Appelle et affiche le résultat de la méthode AbcStr().   |    Calls and displays result from AbcStr method().
  
  ; Appel de la méthode avec paramètres:   -   Calling a method with parameters:
  Debug "Result of AbcBox: " + *MyObjectChild2\Methods\AbcBox("Information", "Hello, world!")
  
;   ; Pour supprimer un objet, suivez scrupuleusement cette méthode !
;   ; To delete an object, follow this method carefully !
;   If DeleteObject(*MyObjectChild2)
;     *MyObjectChild2 = #NUL
;     Debug "Object deleted !" 
;   EndIf
  
Else
  Debug "Error: Uninitialized objects: " + Str(*MyObjectChild2)
  
EndIf

If *MyObjectChild1\Methods\AddChild(*MyObjectChild2) ; Non réfférencé dans les méthodes de l'objet mais accepté.  |  Not re-referenced in the object's methods but accepted.
  Debug "Info: Add Child objects: " + Str(*MyObjectChild2) + " as Parent Object: " + Str(*MyObjectChild2\Parent)
EndIf

; Pour supprimer un objet, suivez scrupuleusement cette méthode !
; To delete an object, follow this method carefully !
If DeleteObject(*MyObjectRoot)
  *MyObjectRoot = #NUL
  Debug "Object deleted !" 
EndIf
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
SMaag
Enthusiast
Enthusiast
Posts: 302
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: [Rename] Object-oriented programming simulation for a virtual object manager

Post by SMaag »

What do you mean with referenced?

I tried in the PB6.20 IDE: AddChild() is part of the selction at intellisense field!
Maybe restart IDE


Some comments on your code.

Now you are doing real OOP. A little away from what is common sense in OOP programming.
(You said you don't like this - maybe you are short before a general mind change! :wink: )
(Ce moment fou où tu réalises que ton esprit était juste bloqué! :shock: - translated by google)

If this is the base of your "object manger", then you try to program an OOP Tree-Structure.

Maybe you did not know this before!


List *Children.ObjectProperties() - better to use Map. In a list it is hard to find an entry because SelectElement() will cause to much problems.
If ForEach is the only function you need, a list is perfect!
ShadowStorm
Enthusiast
Enthusiast
Posts: 303
Joined: Tue Feb 14, 2017 12:07 pm

Re: [Rename] Object-oriented programming simulation for a virtual object manager

Post by ShadowStorm »

Even if I restart PB, it doesn't work!
I've already had this kind of problem not long ago on another test, it's incomprehensible!

https://drive.google.com/file/d/1JV9wNg ... sp=sharing

As I said, I'm doing some tests and then I'll see what I can do, I still have a lot of work to do and
I'm really struggling, so thank you for your help, which means a lot to me.

Sorry Smaag, but I don’t think I ever said I disliked OOP?

I like it, but it’s complicated to implement, and I’m not sure how to use it properly. Still, I appreciate the concept, even though I’ve never really done it or understood how it works in other languages!

I love how it works, for example:

Car = CreateCar()

Car.Paint.Color = Red
CarColor = Car.Paint.Color

Car.Engine.Start()
Car.Engine.Stop()

Car = DestroyCar()

But we can’t do this in PureBasic. So I want to create something as close as possible to this, and that’s simple to use. For now, I’m satisfied with my approach because I want a well-defined style. Do you get it now?

So, if I want to use it with my upcoming virtual object manager, I want something I understand and enjoy using!

Do you understand?
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [Rename] Object-oriented programming simulation for a virtual object manager

Post by mk-soft »

PureBasic is not object-orientated.

You have to look for another language and learn it. PureBasic supports the standard interface for OOP to access COM objects.
It is sufficient for simple OOP in PureBasic. What you want to do is pure OOP and PureBasic will never become an OOP language (luckily)

You have to deal with .Net and C#. an others !!!
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
ShadowStorm
Enthusiast
Enthusiast
Posts: 303
Joined: Tue Feb 14, 2017 12:07 pm

Re: [Rename] Object-oriented programming simulation for a virtual object manager

Post by ShadowStorm »

mk-soft wrote: Mon Feb 24, 2025 9:46 pm PureBasic is not object-orientated.

You have to look for another language and learn it. PureBasic supports the standard interface for OOP to access COM objects.
It is sufficient for simple OOP in PureBasic. What you want to do is pure OOP and PureBasic will never become an OOP language (luckily)

You have to deal with .Net and C#. an others !!!
No thanks, I'm not interested, and this doesn't help to understand why it doesn't work as expected, a bug perhaps?
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
ShadowStorm
Enthusiast
Enthusiast
Posts: 303
Joined: Tue Feb 14, 2017 12:07 pm

Re: [Rename] Object-oriented programming simulation for a virtual object manager

Post by ShadowStorm »

Even if I add other procedures, whatever, it doesn't work, and I've even tried removing
AbcBox.s(Title.s, Message.s), to see if the problem came from there, in short, it doesn't work for me!

Code: Select all

Interface ObjectMethods ; Les Noms des procédures doivent être identiques à ceux du code, avec les mêmes types et les mêmes paramètres.
                        ; The names of the procedures must be identical to those in the code, with the same types and the same parameters.
  Abc123.i()
  Abc246.i()
  AbcXY.i()
  AbcStr.s()
  AbcBox.s(Title.s, Message.s)
  
  AddChild.b(*Child.StructureObject) ; does not work
  AddYXZ2.b() ; does not work
  AddAsdr.i() ; does not work
  
EndInterface

Code: Select all

DataSection
  VirtualTableObject:
  Data.i @Abc123()
  Data.i @Abc246()
  Data.i @AbcXY()
  Data.i @AbcStr()
  Data.i @AbcBox()
  Data.i @AddChild()  ; does not work
  Data.i @AddYXZ2() ; does not work
  Data.i @AddAsdr() ; does not work
EndDataSection
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
ShadowStorm
Enthusiast
Enthusiast
Posts: 303
Joined: Tue Feb 14, 2017 12:07 pm

Re: [Rename] Object-oriented programming simulation for a virtual object manager

Post by ShadowStorm »

I've tried to use it in the object creation for the methods instead:

Code: Select all

*Object\Methods  = AllocateMemory(SizeOf(ObjectMethods))
After all, ObjectMethods is an interface, not a structure, so maybe that's where the problem lies!
No changes.
Last edited by ShadowStorm on Tue Feb 25, 2025 2:27 am, edited 1 time in total.
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
User avatar
Mijikai
Addict
Addict
Posts: 1517
Joined: Sun Sep 11, 2016 2:17 pm

Re: [Rename] Object-oriented programming simulation for a virtual object manager

Post by Mijikai »

ShadowStorm wrote: Mon Feb 24, 2025 10:29 pm I've tried to use it in the object creation for the methods instead:

Code: Select all

*ObjectMethods = AllocateMemory(SizeOf(ObjectMethods))
Do you supply the function pointers, setup the vtable?
ShadowStorm
Enthusiast
Enthusiast
Posts: 303
Joined: Tue Feb 14, 2017 12:07 pm

Re: [Rename] Object-oriented programming simulation for a virtual object manager

Post by ShadowStorm »

It also works like this, but it doesn't change anything, as the methods are not listed in the object.

Code: Select all

; ======================================================
; Création d'un nouvel Objet.
; Creating a new Object.
; ======================================================
Procedure.i NewObject()
  
  ; Déclare un Pointeur (*Object) protégé de type StructureObject, qui prend comme valeur l'adresse de la structure "StructureObject".
  ; Ensuite, alloue un nouvel Objet de type Structure dynamique.
  ; 
  ; Declares a protected Pointer (*Object) of type StructureObject, which takes as a value the address of the "StructureObject" structure.
  ; Then, allocates a new Object of dynamic Structure type.
  Protected *Object.StructureObject = AllocateStructure(StructureObject)
  
  ; Si l'Objet *Object est bien initialisé.
  ; If the Object *Object is properly initialized.
  If *Object.StructureObject
    
    ; Définit la table virtuelle de l'objet pour référencer les méthodes de celui-ci.
    ; Defines the virtual table of the object to reference its methods.
    *Object\VirtualTableObject = ?VirtualTableObject
    
    ; Alloue un nouvel Objet "Properties" de type Structure dynamique qui prend comme valeur l'adresse de la structure "ObjectProperties".
    ; Allocates a new "Properties" Object of dynamic Structure type which takes as a value the address of the "ObjectProperties" structure.
    *Object\Properties = AllocateStructure(ObjectProperties)
    
    ; Si l'Objet "Properties" n'est pas initialisé.
    ; If the "Properties" Object is not initialized.
    If Not *Object\Properties
      Debug "Error: Unable To allocate Object properties."
      FreeStructure(*Object) ; Supprime l'Objet car il a pu être créé.   |   Remove the Object as it has been created.
      ProcedureReturn 0
    EndIf
    
    ; Affecte un Numéro d'identification à l'Objets en fonction de la variable NextObjectID.i.
    ; Assigns an identification number to the object according to the NextObjectID.i variable.
    *Object\Properties\ID = NextObjectID.i
    NextObjectID.i + 1
    
    *Object\Methods = *Object
    
    ; Retourne un pointeur qui est l'adresse mémoire de la structure "StructureObject".
    ; Returns a pointer which is the memory address of the "StructureObject" structure.
    ProcedureReturn *Object
    
  Else
    ProcedureReturn 0
    
  EndIf
  
EndProcedure
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
ShadowStorm
Enthusiast
Enthusiast
Posts: 303
Joined: Tue Feb 14, 2017 12:07 pm

Re: [Rename] Object-oriented programming simulation for a virtual object manager

Post by ShadowStorm »

This works, but my code doesn't. Go figure ! :evil: :twisted:

Code: Select all

Interface NewRectangle
  Perimeter.i()
  Surface.i()
  Length.i(Valeur)
  Width.i(Valeur)
  Destroy.i()
  Abc1.i()
  Abc2.i()
  Ab3.i()
  Abc4.i()
  Abc5.i()
  Abc6.i()
EndInterface

Structure StructureObject
  
  *SObject.NewRectangle
  
EndStructure

Structure Rectangle
  *DSVirtualTable ;Data Section Virtual Table
  
  Length.i
  Width.i
EndStructure

Procedure.i RectangleInit(Length=0, Width=0)
  Protected *Object.Rectangle
  
  *Object = AllocateStructure(Rectangle)
  
  If *Object
    *Object\DSVirtualTable = ?Class
    
    *Object\Length = Length
    *Object\Width = Width
  EndIf
  
  ProcedureReturn *object
EndProcedure


Procedure Perimeter(*this.Rectangle)
  ProcedureReturn (*this\Length + *this\Width) * 2
EndProcedure

Procedure Surface(*this.Rectangle)
  ProcedureReturn *this\Length * *this\Width
EndProcedure

Procedure Length(*this.Rectangle, Valeur)
  *this\Length = Valeur
EndProcedure

Procedure Width(*this.Rectangle, Valeur)
  *this\Width = Valeur
EndProcedure

Procedure Destroy(*this.Rectangle)
  FreeStructure(*this)
EndProcedure

Procedure.i Abc1(*this.Rectangle)
  ProcedureReturn 1
EndProcedure

Procedure.i Abc2(*this.Rectangle)
  ProcedureReturn 2
EndProcedure

Procedure.i Abc3(*this.Rectangle)
  ProcedureReturn 3
EndProcedure

Procedure.i Abc4(*this.Rectangle)
  ProcedureReturn 4
EndProcedure

Procedure.i Abc5(*this.Rectangle)
  ProcedureReturn 5
EndProcedure

Procedure.i Abc6(*this.Rectangle)
  ProcedureReturn 6
EndProcedure

DataSection
  Class:
  Data.i @Perimeter()
  Data.i @Surface()
  Data.i @Length()
  Data.i @Width()
  Data.i @Destroy()
  Data.i @Abc1()
  Data.i @Abc2()
  Data.i @Abc3()
  Data.i @Abc4()
  Data.i @Abc5()
  Data.i @Abc6()
EndDataSection

; How to use it
MyField.NewRectangle = RectangleInit(20,10)

*MyObject.StructureObject
*MyObject = AllocateStructure(StructureObject)

*MyObject\SObject = MyField.NewRectangle

Debug "Perimeter is  " + *MyObject\SObject\Perimeter()
Debug "Surface is  "  + *MyObject\SObject\Surface()

Debug ""

;Let's modify some values
*MyObject\SObject\Length(40)
*MyObject\SObject\Width(20)
Debug "Perimeter is  " + *MyObject\SObject\Perimeter()
Debug "Surface is  "  + *MyObject\SObject\Surface()

*MyObject\SObject\Destroy()
Debug ""
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
ShadowStorm
Enthusiast
Enthusiast
Posts: 303
Joined: Tue Feb 14, 2017 12:07 pm

Re: [Rename] Object-oriented programming simulation for a virtual object manager

Post by ShadowStorm »

Mijikai wrote: Mon Feb 24, 2025 10:49 pm
ShadowStorm wrote: Mon Feb 24, 2025 10:29 pm I've tried to use it in the object creation for the methods instead:

Code: Select all

*ObjectMethods = AllocateMemory(SizeOf(ObjectMethods))
Do you supply the function pointers, setup the vtable?
An error on my part, I've corrected my message thank you.
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
Post Reply