v2 Nested Class definition Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

v2 Nested Class definition

Post by JJohnston2 » 16 Jun 2023, 16:06

Trying to use some v1 code in v2 (with minimal changes where needed).

The v1 code has a nested class Encrypt defined inside of class Crypt, and some methods (including StrEncrypt()) defined inside of class Encrypt... see excerpt below.

User code: hash := Crypt.Encrypt.StrEncrypt(input:="123",pw:="test",CryptAlg:=5,HashAlg:=1)

Error: This value of type "Class" has no method named "StrEncrypt"

Code Excerpt (with method definition statements)
class Crypt
{
class Encrypt
{
static StrEncoding := "UTF-16"
static PassEncoding := "UTF-16"

StrDecryptToFile(EncryptedHash,pFileOut,password,CryptAlg:=1, HashAlg:=1)
...

FileEncryptToStr(pFileIn, password, CryptAlg:=1,HashAlg:=1)
...

StrEncrypt(stringArg,password,CryptAlg:=1,HashAlg:=1)
...

etc.

(Full library here: Crypt.ahk)

I'm unclear if there is a difference in the way classes and nested classes are defined between v1 and v2, or if there was possibly an implicit relationship between the class and nested class in v1, which potentially no longer exists in v2, i.e., "Nesting a class does not imply any particular relationship to the outer class." (from v2 Objects help under Nested Classes sub-topic).

I'm looking for any clues or pointers on modifications that might be needed to get the class structure working in v2 for this library.

A minimal v2 example with a nested class might also point me in the right direction... I haven't found one yet that correlates to the type of usage shown above.

Thanks in advance...

ntepa
Posts: 439
Joined: 19 Oct 2022, 20:52

Re: v2 Nested Class definition  Topic is solved

Post by ntepa » 16 Jun 2023, 16:35

Code: Select all

class Crypt {
    class Encrypt {
        ; Crypt.Encrypt.Prototype.StrEncrypt()
        StrEncrypt() {
            MsgBox "instance method"
        }

        ; Crypt.Encrypt.StrEncrypt()
        static StrEncrypt() {
            MsgBox "static Method"
        }
    }
}

; Instance methods are contained inside the class Prototype property.
; It can be called directly like this.
Crypt.Encrypt.Prototype.StrEncrypt()

; Normally, instance methods are called from a class instance.
inst := Crypt.Encrypt()
inst.StrEncrypt()

; Static methods can be called directly.
Crypt.Encrypt.StrEncrypt()

lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: v2 Nested Class definition

Post by lexikos » 19 Jun 2023, 03:43

As demonstrated by ntepa, the issue is to do with instance vs. static properties, not class nesting.

The documentation explains all of the differences between v1 and v2 with regard to objects/classes (and hopefully all other topics).
https://www.autohotkey.com/docs/v2/v2-changes.htm#objects

For this script, I think there are unlikely to be any differences that matter (considering only class nesting).
if there was possibly an implicit relationship between the class and nested class in v1
No, there was never any such implicit relationship. If there was, the change would be documented above.

The v2 documentation is generally newer, and therefore more likely to cover for misconceptions that have come up over time.

JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: v2 Nested Class definition

Post by JJohnston2 » 19 Jun 2023, 16:36

Thanks both for replies. The simplified example was particularly useful and marking the classes static enabled the calling notation (in the original code, which I didn't write), to remain the same.

Post Reply

Return to “Ask for Help (v2)”