Help with a custom object.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DiGiTaL-CORE
Posts: 2
Joined: 17 Apr 2017, 12:54
Contact:

Help with a custom object.

19 Oct 2018, 12:31

Hello everyone,

I'm practicing OOP and was trying to create a simple String class. From what I can tell the instance is deleted right after it's created. Perhaps I'm using this incorrectly or I still don't understand, lol. It seems the new instance retains it's value, but no longer keeps it's methods/properties.

Code: Select all

class objString {
; Construction ----------------------------------------------------------------
  __New(Value="") {
    this.Value:=Value
    MsgBox % "__New: " this.Value " (" this.Len ")"
    return Value
  }
; Properties ------------------------------------------------------------------
  Len {
    get {
      return StrLen(this.Value)
    }
  }
; Methods ---------------------------------------------------------------------
  Sub(Pos,Len="") {
    return SubStr(this.Value,Pos,Len)
  }

; Returns first Len characters of object string.
  L(Len=1) {
    return this.Sub(1,Len)
  }

; Returns last Len characters of object string.
  R(Len=1) {
    return this.Sub(this.Len-Len+1,Len)
  }

  Find(String,Case=false,Pos=1,Occurrence=1) {
    return InStr(this.Value,String,Case,Pos,Occurrence)
  }
; Destruction -----------------------------------------------------------------
  __Delete() {
    MsgBox % "__Delete: " this.Value
  }
}

testString:=new objString("Hello, world!")
MsgBox % """" testString """ is " testString.Len " characters long."
MsgBox % """" testString """ is " StrLen(testString) " characters long."
I've always struggled outside of AHK's more linear programming style and apologize if this has been asked before, but I couldn't find any helpful posts.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Help with a custom object.

19 Oct 2018, 12:38

Don't return Value from the constructor. Return the object itself(this) or don't return anything at all
Last edited by swagfag on 19 Oct 2018, 15:24, edited 2 times in total.
DiGiTaL-CORE
Posts: 2
Joined: 17 Apr 2017, 12:54
Contact:

Re: Help with a custom object.

19 Oct 2018, 13:27

swagfag wrote:
19 Oct 2018, 12:38
Don't return 'value' from the constructor. Return the object of don't return anything
Thanks! Now my methods and properties work correctly.

Code: Select all

class objString {
; Construction ----------------------------------------------------------------
  __New(Value="") {
    this.Value:=Value
    MsgBox % "__New: " this.Value " (" this.Len ")"
  }

; Properties ------------------------------------------------------------------
  Len {
    get {
      return StrLen(this.Value)
    }
  }
; Methods ---------------------------------------------------------------------
  Sub(Pos,Len="") {
    return SubStr(this.Value,Pos,Len)
  }

; Returns first Len characters of object string.
  L(Len=1) {
    return this.Sub(1,Len)
  }

; Returns last Len characters of object string.
  R(Len=1) {
    return this.Sub(this.Len-Len+1,Len)
  }

  Find(String,Case=false,Pos=1,Occurrence=1) {
    return InStr(this.Value,String,Case,Pos,Occurrence)
  }
; Destruction -----------------------------------------------------------------
  __Delete() {
    MsgBox % "__Delete: " this.Value
  }
}

testString:=new objString("Hello, world!")
MsgBox % """" testString """ is " testString.Len " characters long."
testString.Value:="Hello, new world!"
MsgBox % """" testString """ is " testString.Len " characters long."

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Joey5, RandomBoy and 321 guests