How to reference current instance of an object

Questions regarding syntax

How to reference current instance of an object

Postby bobr » Thu Aug 25, 2011 11:57 am

HELP!
I'm trying to build a linked list. I'm trying to use objects for the cells in the list. I'm not a proficient object programmer. In the past, I've built such lists in 'C' using structures. This works very nicely. Now I'm trying to move everything into KBasic and am having difficulties because I don't fully understand what I'm doing!
My problem appears to be with referencing my current object. This is a bit of the code:


class ListHead
Dim ListLength as integer
Dim FirstCell as Node
Dim LastCell as Node

constructor ListHead
ListLength = 0
FirstCell = Null
LastCell = Null
end constructor

destructor ListHead
ListLength = 0
FirstCell = Null
LastCell = Null
end destructor
end class

class Node
Dim Info as Item
Dim NextNode as Node

constructor Node
Info = Null
NextNode = Null
end constructor

destructor Node
Info = Null
NextNode = Null
end destructor

Function DeleteNode(ListPtr as ListHead) as boolean
Dim NewPtr as Node = new Node
Dim ThisPtr as Node = new Node

ThisPtr = me
'Find preceding cell
NewPtr = ListPtr.FirstCell
Do While NewPtr <> NULL
if NewPtr.NextNode = ThisPtr then exit Do ' <<---------- this is the line I'm having problems with *****************!!!!!
NewPtr = NewPtr.NextNode
Loop
if NewPtr = NULL then return FALSE
NewPtr.Next = CellPtr.Next ' unlink cell
ListPtr.Length = ListPtr.Length - 1 'reduce the list length
if ListPtr.Length = 0 then ListPtr.Last = NULL
return TRUE
End Function
end class

My problem is that whatever I do to try to compare a cell pointer with the current instance of the object I get an 'incompatible types error' on this line of code. It was easy with 'C', you just compare pointers! With this though, I've tried just about everything I can think of (including various usages of 'Me') to compare the pointer reference held in NewPtr.NextNode with the current cell reference and nothing seems to work.

Please, one of you clever people out there, what am I doing wrong?
bobr
 
Posts: 31
Joined: Fri Mar 18, 2011 10:30 pm

Re: How to reference current instance of an object

Postby bobr » Fri Aug 26, 2011 6:19 pm

Sorry, there were a few basic coding errors in my last post but, notwithstanding that, I really can't understand how to compare objects. Following the KISS principle, and taking one of the class examples from the distributed software (and changing it very slightly):

Class Dict
Type node
item As Integer
info As Integer
End Type

Public a As node

Constructor Dict()
a.item = 1234
a.info = 6789
End Constructor

Destructor Dict()
Print "Dict destructor"
End Destructor

End Class

Dim d As New Dict
CLS
if d = d then print "d=d" '<--- this creates an incompatible types error
Print d.a.item; d.a.info

End

According to the manual, I can compare object variables to test to see if they reference the same object. So why, when I do the above, do I get an "incompatible types" error. I must be misunderstanding the manual because I'm just comparing an object variable with itself!
bobr
 
Posts: 31
Joined: Fri Mar 18, 2011 10:30 pm

Re: How to reference current instance of an object

Postby bobr » Sun Sep 18, 2011 11:09 am

For anyone who has looked at this thread and, like me, was totally flawed by how to compare object references, this is the solution I have worked out:

Supposing you want to compare two object references, say object1 and object2, to say if they both refer to the same object. You can't simply say:
if object1 = object2 then print "they're the same"

This gives you a "type" error

What you have to do is to write a function to do the comparison. Like this:
Function CompareObjects(ObjectRef1 as ObjectRef, ObjectRef2 as ObjectRef) as Boolean ' where ObjectRef is the user defined type or class
Dim a as Object
Dim b as Object
a = Object1Ref
b = Object2Ref
if a = b then return TRUE
else return FALSE
end if
end Function

You can then compare the object references by:
if CompareObects(Object1, Object2) then print "they're the same"

If anyone knows of a better way of doing this, please post a reply. I've also tried to use a cast - if cast.object(Object1) = cast.object(Object2) then . . .
but this just raises a syntax error. Of course, I may be misunderstanding how to use 'cast'!
bobr
 
Posts: 31
Joined: Fri Mar 18, 2011 10:30 pm

Re: How to reference current instance of an object

Postby berndnoetscher » Mon Sep 19, 2011 7:23 am

Well done. Thanks for your posts.
berndnoetscher
Site Admin
 
Posts: 1059
Joined: Tue Sep 25, 2007 9:37 am


Return to Coding Questions

cron