Look at the Counter property for a description of the bug.
Code: Select all
Class Class02
{
static CurVer := 1.1, Instances := 0, MaxInstancesAllowed := 3, Cnt := 0
Instance := 0
EM := ""
__New()
{
this.Instance := ++Class02.Instances
if (this.Instance > Class02.MaxInstancesAllowed)
{
this.EM := "You attempted to create instance " . this.Instance . " when only " . Class02.MaxInstancesAllowed . " instances are allowed!`n`nInstance was not created!"
return null
}
MsgBox % "Instance " . this.Instance . " was created!"
}
__Delete()
{
MsgBox % this.EM = "" ? "Instance " . this.Instance . " is being deleted!" : this.EM
Class02.Instances -= 1
}
Version
{
get
{
return Class02.CurVer
}
}
Counter
{
get
{
; **************************************************************************** BUG
; ; Line ; *
Class02.Cnt += 1 ; A ; *
this.Cnt += 1 ; B ; *
;return Class02.Cnt ; Returns 1, 2, 3 correctly. ; C ; *
return this.Cnt ; Returns 2, 3, 4 This is incorrect! ; D ; *
; How did this.Cnt start off being 2? My guess is that it inheits the ; *
; value of Class02.Cnt at the time it is first used. That is why ; *
; changing the order of lines A and B give different results. ; *
; This could cause hard to track down bugs. ; *
; If you reverse the order of lines A and B, both return correct values. ; *
; Uncommet line C or D and comment the other. ; *
; ; *
; While on the subject, having to use the class name within its own ; *
; property or method is not good, there should be no external names ; *
; used within an instance to access any of the class' variables. I've ; *
; never seen this before. My thought on the matter is that this.Cnt ; *
; would always reference the static variable, just like in other ; *
; languages, and where a static variable exists, no instance variable of ; *
; the same name should be created. They are all part of the same class, ; *
; static or instance. If the developer needs a copy of the static ; *
; variable at the time the class is instantiated, he can simply make a ; *
; copy in the ___New() method which is called when instantiated. If ; *
; what I think happened is true, that __New() is a later addition and ; *
; there was no way to get a copy of the static variables at the time it ; *
; was instantiated, I understand why things work the way they do. But, ; *
; I hope this isn't carried into V2, but rather corrected to be like ; *
; most, if not all, other languages implementing classes. ; *
; ****************************************************************************
}
}
MyInstance
{
get
{
return this.Instance . " of " . this.MaxInstancesAllowed
}
}
GetInstance()
{
MsgBox % "This is instance " . this.Instance . " of " . Class02.Instances . " instances."
}
}
c01 := New Class02
c02 := New Class02
c03 := New Class02
c04 := New Class02
MsgBox % "Version: " . c01.Version . ", Instance: " . c01.MyInstance . ", Counter: " . c01.Counter
MsgBox % "Version: " . c02.Version . ", Instance: " . c02.MyInstance . ", Counter: " . c02.Counter
MsgBox % "Version: " . c03.Version . ", Instance: " . c03.MyInstance . ", Counter: " . c03.Counter
MsgBox % "Version: " . c04.Version . ", Instance: " . c04.MyInstance . ", Counter: " . c04.Counter
MsgBox All Instances have been created.