X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 19 Apr 2007 04:09 PM by  anon
Object oriented class
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
19 Apr 2007 04:09 PM
    I want to create a typical object oriented class with a class member and getter/setter for that member. I've written the code below, which compiles but does not run. Is IDL OO enought to do what I want to do? If so, how would write it? and instantiate it? Thanks. ; method increments the number FUNCTION test::incrementNumber self.number = self.number + 1 RETURN,1 END ; Method returns the number FUNCTION test::getNumber RETURN, self.number END ; method sets the number FUNCTION test::setNumber, n self.number = n RETURN,1 END ; Object definition. PRO test__define struct = {test, $ number:999 $ } END

    Deleted User



    New Member


    Posts:
    New Member


    --
    19 Apr 2007 04:09 PM
    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
    You are not authorized to post a reply.