OOP Question

BlitzMax Forums/BlitzMax Programming/OOP Question

maverick69(Posted 2005) [#1]
Ist it possible to destroy a instance from a Type from within a Type-Method, for example, something like this

Type A
  Field x:int
  Method Check()
    if (x<0) Then Self = Null
  End Method
End Type

MyObject:A = New A

While (Not Keydown (KEY_ESCAPE))
   MyObject.Check()
Wend




ziggy(Posted 2005) [#2]
You can dereference it, and then it will die at the next flushmem
Type A
  Field x:int
  Method Check()
    if (x<0) Then 
       MyObject = Null
       Return
    endIf
  End Method
End Type

MyObject:A = New A

While (Not Keydown (KEY_ESCAPE))
   If Not MyObject = Null then MyObject.Check()
   flushMem
Wend


It usualy has no sense to check for a single instance of an object inside the object itself.
Anyway, the object can remove itself from a List or collection if it's contained there. If there are not more instances of the object, it will die when it's dereferenced from the List or collection.
In my honest opinion, objects shouldn't be referenced twice. It's always a sign of poor design.