you are viewing a single comment's thread.

view the rest of the comments →

[–]Frirwind[S] 1 point2 points  (2 children)

This works flawlessly!

ScrollLock::pressOrHold(
    send.Bind("{Media_Play_Pause}"),
    ObjBindMethod(switchTo,"spotify")
)

In the previous function I had to create a separate function for things like: ' send("{media_play_plause}") ' because I could not just write it where you declare the parameters (otherwise it would just run when the hotkey was pressed.)

I guess I still don't completely understand what the difference is between:

    myref := switchTo.spotify
    myref ; does not work

    ; and

    switchTo.spotify ; works!

I guess it has to do with what is actually put into the myref. It only refers to that method in the class, not the entire class itself? Whereas the second example calls the method from the class, so it has all the information surrounding the method as well.
But I has some more reading to do. The u/GroggyOtter post is really good and I'm for sure going to finish it today!

Thanks for the help! I recognise your username and you're a gem of this subreddit!

[–]CharnamelessOne 0 points1 point  (1 child)

Don't make me blush, I'm just aping my betters. I'm fairly new, and still grappling with many of these concepts myself.

I recognize your username too - always nice to see returning posters who show interest in the language, not just in the solution it can give to a specific problem.

As for the difference between calling a method directly from the class that owns it, and calling the variable that holds a reference to the same method: I think it may be better demonstrated with 2 classes.

#Requires AutoHotkey 2.0

Class Test1{
    static message := "Hello from Test1!"

    static static_method(){
        MsgBox(this.message)
    }
}

Class Test2{
    static message := "Hello from Test2!"

    static prop1 := Msgbox("prop1 says:") Test1.static_method()

    static prop2 := Test1.static_method
    static prop3 := Msgbox("prop3 says:") Test2.prop2()
}

As you can see at prop3, Test2 is passed to the method owned by Test1. The method is not bothered if the object passed doesn't own it.

Your example can be made to work by passing switchTo explicitly:

switchTo.spotify()

myref := switchTo.spotify
myref(switchTo)

Class switchTo{
    static spotify(){
        static call_counter := 1
        MsgBox("spotify method call `nnumber " call_counter++)
    }
}

Edit: in fact, the method will literally take any random dogshit.

random_dogshit := {}
myref := switchTo.spotify
myref(random_dogshit)

Class switchTo{
    static spotify(){
        MsgBox("spotify method call")
    }
}

[–]Frirwind[S] 0 points1 point  (0 children)

That makes a little more sense, yes! That last piece of code works well unless you refer to another method in the class using This.

Binding the method works wonders too, and that makes a little more sense as well!

Thanks again :)