Jump to content

Recommended Posts

Posted

This thread has revolutionized the way I have created some of my recent commercial projects.
Thanks, @guinness

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)
  On 7/9/2015 at 6:54 PM, guinness said:

I posted this the other day, but thought I would post in a separate topic instead.

#include <MsgBoxConstants.au3>

; ---- Start of Person Class

; Stored in the 'object' to verify it's our 'object' and not some random array
Global Const $PERSON_GUID = '4197B285-6AB1-489B-8585-08C852E33F3D'

; Friendly names for 0, 1, 2 and 3
Global Enum $PERSON_AGE, $PERSON_NAME, $PERSON_ID, $PERSON_MAX

; Constructor
Func Person($sName, $iAge)
    Local $hPerson[$PERSON_MAX]

    ; Set the GUID, so as the verification will work
    $hPerson[$PERSON_ID] = $PERSON_GUID

    Person_SetAge($hPerson, $iAge)
    Person_SetName($hPerson, $sName)

    ; Return the Person 'object'
    Return $hPerson
EndFunc   ;==>Person

; Getter for the age property
Func Person_GetAge(ByRef $hPerson)
    Return _Person_IsObject($hPerson) ? $hPerson[$PERSON_AGE] : Null
EndFunc   ;==>Person_GetAge

; Setter for the age property
Func Person_SetAge(ByRef $hPerson, $iAge)
    ; If not a valid 'object' or integer then return
    If Not _Person_IsObject($hPerson) Or Not IsInt($iAge) Then Return

    ; Set the age
    $hPerson[$PERSON_AGE] = $iAge
EndFunc   ;==>Person_SetAge

; Getter for the name property
Func Person_GetName(ByRef $hPerson)
    Return _Person_IsObject($hPerson) ? $hPerson[$PERSON_NAME] : Null
EndFunc   ;==>Person_GetName

; Setter for the name property
Func Person_SetName(ByRef $hPerson, $sName)
    ; If not a valid 'object' then return
    If Not _Person_IsObject($hPerson) Then Return

    ; Set the name
    $hPerson[$PERSON_NAME] = $sName
EndFunc   ;==>Person_SetName

; ToString() for the 'object'
Func Person_ToString(ByRef $hPerson)
    Return _Person_IsObject($hPerson) ? StringFormat('Name: %s, Age: %i', $hPerson[$PERSON_NAME], $hPerson[$PERSON_AGE]) : Null
EndFunc   ;==>Person_ToString

; Check if it's a valid 'object' and not some random array. "NTERNAL ONLY!
Func _Person_IsObject(ByRef $hPerson)
    Return UBound($hPerson) = $PERSON_MAX And $hPerson[$PERSON_ID] == $PERSON_GUID
EndFunc   ;==>_Person_IsObject

; ---- End of Person Class

Example()

Func Example()
    ; Store the Person 'object', which is just a glorified array
    Local $hP1 = Person('John', 30)

    ; Display the 'object'
    MsgBox($MB_SYSTEMMODAL, 'Person 1', Person_ToString($hP1))

    ; Create a new person

    ; Store the Person 'object', which is just a glorified array
    Local $hP2 = Person('James', 36)

    ; Display the 'object'
    MsgBox($MB_SYSTEMMODAL, 'Person 2', Person_ToString($hP2))

    ; Set the age for Person 2
    Person_SetAge($hP2, 45)

    ; Display the 'object'
    MsgBox($MB_SYSTEMMODAL, 'Person 2 - Revised', Person_ToString($hP2))
EndFunc   ;==>Example

 

Expand  

This coding style is really really what I did in my last project. it even much more radical than this... I see you stop here using the "$g_". good idea.. this is a bad practices in my opinion

Edited by Guest
Posted

I don't use $g_ because there are no global variables, only constants.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

I saw that. I also believe you do not want g_ and you found a good excuse not to use it in this example.

Not using "g_" allows you to implement this approach better in a way that look more clean and make sence.. Once you have decided to divide the code (into sections) like this, it make more sence to do this also to global variables and this is instead of using "g_" as their prefix...

 

These are exactly the guidelines

That I followed when I made this project

You can call it also "OOP-like approach" but it's more extreme than the example. 

My guidelines are also to use the #Region & #EndRegion, the TAB, #cs #ce in very high frequency (I did not mention this in the guidelines)

 

 

Edit:

My advice is to write it this way with the #Region & #EndRegion

#include <MsgBoxConstants.au3>

#Region Person Class
    ; Stored in the 'object' to verify it's our 'object' and not some random array
    Global Const $PERSON_GUID = '4197B285-6AB1-489B-8585-08C852E33F3D'


    ; Friendly names for 0, 1, 2 and 3
    Global Enum $PERSON_AGE, $PERSON_NAME, $PERSON_ID, $PERSON_MAX

    ; Constructor
    Func Person($sName, $iAge)
        Local $hPerson[$PERSON_MAX]

        ; Set the GUID, so as the verification will work
        $hPerson[$PERSON_ID] = $PERSON_GUID

        Person_SetAge($hPerson, $iAge)
        Person_SetName($hPerson, $sName)

        ; Return the Person 'object'
        Return $hPerson
    EndFunc   ;==>Person

    ; Getter for the age property
    Func Person_GetAge(ByRef $hPerson)
        Return _Person_IsObject($hPerson) ? $hPerson[$PERSON_AGE] : Null
    EndFunc   ;==>Person_GetAge

    ; Setter for the age property
    Func Person_SetAge(ByRef $hPerson, $iAge)
        ; If not a valid 'object' or integer then return
        If Not _Person_IsObject($hPerson) Or Not IsInt($iAge) Then Return

        ; Set the age
        $hPerson[$PERSON_AGE] = $iAge
    EndFunc   ;==>Person_SetAge

    ; Getter for the name property
    Func Person_GetName(ByRef $hPerson)
        Return _Person_IsObject($hPerson) ? $hPerson[$PERSON_NAME] : Null
    EndFunc   ;==>Person_GetName

    ; Setter for the name property
    Func Person_SetName(ByRef $hPerson, $sName)
        ; If not a valid 'object' then return
        If Not _Person_IsObject($hPerson) Then Return

        ; Set the name
        $hPerson[$PERSON_NAME] = $sName
    EndFunc   ;==>Person_SetName

    ; ToString() for the 'object'
    Func Person_ToString(ByRef $hPerson)
        Return _Person_IsObject($hPerson) ? StringFormat('Name: %s, Age: %i', $hPerson[$PERSON_NAME], $hPerson[$PERSON_AGE]) : Null
    EndFunc   ;==>Person_ToString

    ; Check if it's a valid 'object' and not some random array. "NTERNAL ONLY!
    Func _Person_IsObject(ByRef $hPerson)
        Return UBound($hPerson) = $PERSON_MAX And $hPerson[$PERSON_ID] == $PERSON_GUID
    EndFunc   ;==>_Person_IsObject

#EndRegion

Example()

Func Example()
    ; Store the Person 'object', which is just a glorified array
    Local $hP1 = Person('John', 30)

    ; Display the 'object'
    MsgBox($MB_SYSTEMMODAL, 'Person 1', Person_ToString($hP1))

    ; Create a new person

    ; Store the Person 'object', which is just a glorified array
    Local $hP2 = Person('James', 36)

    ; Display the 'object'
    MsgBox($MB_SYSTEMMODAL, 'Person 2', Person_ToString($hP2))

    ; Set the age for Person 2
    Person_SetAge($hP2, 45)

    ; Display the 'object'
    MsgBox($MB_SYSTEMMODAL, 'Person 2 - Revised', Person_ToString($hP2))
EndFunc   ;==>Example

 

Option 2 is to write every Person_* inside "Person.au3" file
and every global variable of "Person" write inside "Person.Globals.au3" and in the main script - include Person.Globals.au3 above Person.au3.. That's what I did. you may have many au3 files of "*.Globals" and in that case include them like this:

#Region Common includes
    #include <Array.au3>
    #include <WinAPI.au3>
    ; ......
#EndRegion


#Region objects_Globals
    #include 'Person.Globals.au3'
    #include 'School.Globals.au3'
    #include 'Teacher.Globals.au3'
    #include 'Officer.Globals.au3'
#EndRegion

#Region objects
    #include 'Person.au3'
    #include 'School.au3'
    #include 'Teacher.au3'
    #include 'Officer.au3'
#EndRegion

 

Think .. maybe at first you do not understand why. But later this will be clearer

Edited by Guest

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...