0

Good day, I'm trying to improve my OOP competence and trying to create a code to generate a single window(gui) which hold n checkboxes based on the files the code finds in a specific folder. The idea I landed on is to create a single class that represents the window, and from that call n instances of a class that adds the checkboxes. I will also need to save and load the status of those checkboxes from a file.

Class Class_Checkbox{
        toggle := false
        __new(caller,toggle,words){
            if toggle{
                toggle := "Checked"
                this.toggle := true
            }
            local checkbox_ptr := caller.window.AddCheckBox(toggle,words)
            checkbox_ptr.OnEvent("Click",this.ToggleFunction.bind(this))
        }
        ToggleFunction(*){
            this.toggle := !this.toggle
    }
}

Class Class_Module{
    data := Class_DataLink()
    window := gui()
    shownt := true
    names := []
    boxes := []
    __new(path := ".\modules\"){
        this.window.Opt("-Caption ToolWindow")
        Loop Files, path . "*.ahk"{
            this.boxes.push(Class_Checkbox(this,this.data.Load(A_LoopFileName, false),A_LoopFileName))
            this.names.push(A_LoopFileName)
        }
    }
    customReload(){
        for i in this.names{
            this.data.Save(this.boxes[A_Index].toggle,this.names[A_Index])
        }
    }
}

^This is the "full" code I managed, it refers some other classes (DataLink) to retrieve if the checkbox to be created needs to be already checked. Since I've no idea how to create a gui from scratch, or a checkbox, or a click event I want to be a good OOProgrammer, I'm trying to rely on the Gui elements ahk provides.

I've two questions tho:

  1. Is this (call a class that calls another class n times) a "good" approach to make a dynamic number of checkboxes, or is there a better way I'm clearly missing? This is considering that I might want to reuse Class_Checkbox in the future.

  2. I managed to link the right function to the checkbox thanks to three elements:

a) checkbox_ptr that holds the reference to the created checkbox (which I discard after use? I hope)

b) using the caller parameter to be able to access the attributes of the caller class

c) .bind(this), which is a little witchcraft atm, but if I understood it right, it assures that the linked function is not linked as a relative "this.ToggleFunction()", which would use the caller class context, but an absolute "this(Class_Checkbox).ToggleFunction()"

Did I understand those elements correctly? And are they all necessary if I want to have two different objects? Here is a reduced/minimal code for this specific question

Class MyOtherClass{
        myAttribute := false
        __new(caller,toggleParam,checkboxLabel){
            if toggleParam{
                toggleParam := "Checked"
                this.myAttribute := true
            }
            local checkbox_ptr := caller.myWindow.AddCheckBox(toggleParam,checkboxLabel)
            checkbox_ptr.OnEvent("Click",this.ToggleFunction.bind(this))
        }
        ToggleFunction(*){
            this.myAttribute := !this.myAttribute
    }
}

Class MyClass{
    myWindow := gui()
    myCheckboxes := []
    __new(path := ".\a_valid_path\"){
        Loop Files, path . "*.ahk"{
            this.myCheckboxes.push(MyOtherClass(this,false,A_LoopFileName))
        }
    }
}

I'm also wondering if this "problem" is specific to AHK, or if I will encounter it in other languages, like Pascal or C++.

I realize this might be a lot to answer, and I hope my poor english won't make this unreadable; thanks to anyone who read 'till here.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.