Your code runs; it just isn't doing what you want it to, I suspect. Proof that it runs is in the following test commands:
IDL> oTest = obj_new('test')
IDL> void = oTest->SetNumber(42)
IDL> void = oTest->IncrementNumber( )
IDL> print, oTest->GetNumber( )
43
Note the following, though:
IDL> oTest2 = obj_new('test')
IDL> print, oTest2->GetNumber( )
0 ; WHY ISN'T THIS 999?!?
What leads to strange behavior in your example is a misconception that TEST__DEFINE can initialize in the {TEST} structure. In fact, initialization of values (at least ones that are not inherited from a superclass) can only occur in the 'Init' method of the class. OBJ_NEW calls 'Init' automatically during its execution. The structure definition behavior in TEST__DEFINE. Although I do not know the underlying algorithm implemented by IDL, I am pretty sure that the values used in the struct definitions in '...__DEFINE' procedures are only used to determine data storage size. The assignment of non-default values into the structure must be done explicitly in the 'Init' method. Default values are probably all 0's, null strings and null pointers.
Thus, the following additional FUNCTION method is called for in your example:
FUNCTION test::Init
self.number = 999
return,1
END
One extra tip: Class methods can be PROcedures. I do not think that you will find any programming advantage to having simple Getter/Setter methods like the ones you are testing be FUNCTIONs rather than PROs.
James Jones
|