• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

"this" keyword usage

Status
Not open for further replies.
Level 15
Joined
Aug 7, 2013
Messages
1,338
Hi,

I am trying to use the "this" keyword for a struct method, but I keep getting a syntax error. I am very confused.

JASS:
    static method toString takes nothing returns string
        return monsterId2Str[this.index]
    endmethod

Error: "this is not a type that allows . syntax"
 
in static methods this is not declared.

You need to have a create method then call it or load the struct from an array / hashtable.

Example:
JASS:
    static method toString takes nothing returns string
        local thistype this = thistype.Create()
        return monsterId2Str[this.index]
    endmethod
or to load from an array.
JASS:
    static method toString takes nothing returns string
        local thistype this = myStructArray[0]
        return monsterId2Str[this.index]
    endmethod
 
JASS:
    static method toString takes nothing returns string
    endmethod

    function toString takes nothing returns string
    endfunction

JASS:
    method toString takes nothing returns string
    endmethod

    function toString takes integer this returns string
    endfunction

also, this. is not needed, any of the following will work

JASS:
this.field
.field
field

however, if you have this scenario, you should use this. or .

JASS:
method hi takes integer i returns nothing
    set this.i = i
    set .i = i
    //set i = i will not work, the local i will be seen
endmethod

for static, you use thistype

JASS:
method hi takes integer i returns nothing
    set thistype.i = i
endmethod

thistype is just sort of a macro that is replaced with the struct's name. It can be used anywhere, including strings.
 
Status
Not open for further replies.
Top