How to check dynamic property exist? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Seven0528
Posts: 334
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

How to check dynamic property exist?

Post by Seven0528 » 29 Jan 2023, 13:46

Code: Select all

#Requires AutoHotkey v1.1
#SingleInstance Force



F2::
    BlockKeybdInput.On()
    Sleep 2000
    BlockKeybdInput.Off()
    return


Class BlockKeybdInput ; v1.1
{ ; No administrator rights required
    On(obj:="ih")    {
        if !this[obj].InProgress    {
            this[obj]:=InputHook()
            this[obj].MinSendLevel:=101
            this[obj].VisibleNonText:=false
            this[obj].Start()
        }
    }
    Off(obj:="ih")    {
        if this[obj].InProgress
            this[obj].Stop()
    }
}

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
BlockKeybdInput:=BlockKeybdInputObj()


F2::
{
    BlockKeybdInput.On()
    Sleep 2000
    BlockKeybdInput.Off()
}

Class BlockKeybdInputObj ; v2.0
{ ; No administrator rights required
    On(obj:="ih")    {
        Try     this.%obj% ;  <-- Error: This value of type "BlockKeybdInput" has no property named "ih".
        Catch    {
            this.%obj%:=InputHook()
        }
        if !this.%obj%.InProgress    {
            this.%obj%.MinSendLevel:=101
            this.%obj%.VisibleNonText:=false
            this.%obj%.Start()
        }
    }
    Off(obj:="ih")    {
        Try     this.%obj%
        Catch    {
            this.%obj%:=InputHook()
        }
        if this.%obj%.InProgress
            this.%obj%.Stop()
    }
}

 Here is my v1 code.
And I tried to convert this v1 code to v2.
I used Try Catch to verify that obj exists, but I don't think this is the right answer.
I am not familiar with v2. Am I right? Please someone fix my ugly code...



Code: Select all

#Requires AutoHotkey v1.1
#SingleInstance Force

F3::
    New_SomeClass:=New SomeClass
    New_SomeClass.Method_1()
    New_SomeClass.Method_2()

    SomeClass.Method_1()
    SomeClass.Method_2()
    ExitApp


Class SomeClass
{
    Method_1()    {
        var1:="str1"
        this[var1]:="STR1"
        this.str2:="STR2"
    }
    Method_2()    {
        var1:="str1"
        Msgbox % this[var1]
        Msgbox % this.str2
    }
}

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

F3::
{
    New_SomeClass:=SomeClass()
    New_SomeClass.Method_1()
    New_SomeClass.Method_2()

    SomeClass().Method_1()
    SomeClass().Method_2()  ;  <-- Error: This value of type "SomeClass" has no property named "str1".
                            ;      Error: This value of type "SomeClass" has no property named "str2".
    ExitApp
}


Class SomeClass
{
    Method_1()    {
        var1:="str1"
        Try     this.%var1%
        Catch
            this.%var1%:="STR1"
        this.str2:="STR2"
    }
    Method_2()    {
        var1:="str1"
        Msgbox this.%var1%
        Msgbox this.str2
    }
}

 Also, I wonder if "this." can no longer be used to share its property
without constructing a new instance of that class.
I'm sorry but I don't speak English well,
so if I missed the relevant explanation in quick reference, please someone let me know that link.


Thank you for reading it:)

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to check dynamic property exist?  Topic is solved

Post by teadrinker » 29 Jan 2023, 19:57

The correct translation to v2 of the second code is this:

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

F3::
{
    New_SomeClass := SomeClass()
    New_SomeClass.Method_1()
    New_SomeClass.Method_2()

    SomeClass.Method_1()
    SomeClass.Method_2()

    ExitApp
}


Class SomeClass
{
    Method_1() {
        var1 := "str1"
        if !this.HasOwnProp(var1)
            this.%var1% := "STR1"
        this.str2 := "STR2"
    }
    Method_2() {
        var1 := "str1"
        Msgbox this.%var1%
        Msgbox this.str2
    }
    static Method_1() {
        var1 := "str1"
        if !this.HasOwnProp(var1)
            this.%var1% := "STR1"
        this.str2 := "STR2"
    }
    static Method_2() {
        var1 := "str1"
        Msgbox this.%var1%
        Msgbox this.str2
    }
}
Classes
If questions remain, please ask.

User avatar
Seven0528
Posts: 334
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: How to check dynamic property exist?

Post by Seven0528 » 29 Jan 2023, 20:40

 A Fantastic Answer! Thank you, teadrinker!!
I didn't realize that Object documents have changed a lot.
Also, it is helpful for translating my v1 library into v2. Thanks once again.

Post Reply

Return to “Ask for Help (v2)”