Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointers to objects 2

Status
Not open for further replies.

jm314

Technical User
Sep 7, 2004
23
US
Anyone know how I can compare 2 pointers to see if they point to the same instance of an object. This is for searching a linked list for the last object in the list. Just comparing them,i.e. "do until listObject1 = listObject2", gives a error. I've also tried "listObject1.nextObject = Nothing" and "listObject1.nextObject = Null" which also give errors. I've been teaching myself how to write code in VBA and can't seem to remember anything about pointers from the other programming languages that I've long forgotten.
 
Hi,

I think of POINTERS in terms of memory addresses.

Does this coincide with what you are doing? If so, how?

Can you state a simple example of this problem?

Skip,

[glasses] [red]Be advised:[/red] When transmitting sheet music...
If it ain't baroque, don't fax it! [tongue]
 
Curious as well. What are you doing? You are making a list of object names...and then...?

Gerry
 
Thats how I think of pointers also, I think. The pointer variable holds the address of the object, right? What I thought is that if two pointers point to the same object, they would both contain the same address and could be compared on that basis.

Here's an example of the code that is giving me a problem:

Class Module
*********************************
'Class name: ListClass

Public Name As String 'the data held by the list "cell"
Public NextRecord As ListClass 'the pointer to the
'next "cell" in the linked list
*********************************

Code Module
*********************************
Sub main()

Dim i As Integer
Dim FirstRecord As ListClass
Dim LastRecord As ListClass
Dim CurrentRecord As ListClass
Dim tempRecord As ListClass

Set FirstRecord = New ListClass
FirstRecord.Name = "A"
Set TempRecord = New ListClass
Set FirstRecord.NextRecord = TempRecord
Set TempRecord = FirstRecord

For i = 66 To 68
Set CurrentRecord = TempRecord.NextRecord
CurrentRecord.Name = Chr$(i)
Set TempRecord = New ListClass
CurrentRecord.NextRecord = TempRecord
Next

Set LastRecord = CurrentRecord

'This is the part giving me the trouble
'with comparing pointers:

Set CurrentRecord = FirstRecord
i = 0
Do Until CurrentRecord = LastRecord 'error here
i = i + 1
Cells( i, 1).Value = CurrentRecord.Name
Set TempRecord = CurrentRecord.NextRecord
Set CurrentRecord = TempRecord
Loop

End Sub
*****************************************
 
I'm thinking of using a linked list to store a list of records instead of using cells on a spreadsheet to store the individual data. This is in case I decide down the road to make an executable with VB instead of keeping the VBA version I'm writing now. I don't have VB compiler right now so thats why I'm using VBA. I may just go with a dynamic array, but I get a kick out figuring out new things so I'm wasting some time with this linked-list idea.
 
Hi,

You have to compare a PROPERTY of the object, I believe
Code:
Do Until CurrentRecord[b].Name[/b] = LastRecord[b].Name[/b] 'error here
  i = i + 1
  Cells( i, 1).Value = CurrentRecord.Name
  Set TempRecord = CurrentRecord.NextRecord
  Set CurrentRecord = TempRecord
Loop



Skip,

[glasses] [red]Be advised:[/red] When transmitting sheet music...
If it ain't baroque, don't fax it! [tongue]
 
Darn, I was hoping that wouldn't be the case as there are bound to be duplicate data stored in the list. Oh well, comparing properties can be worked with. Maybe I'll include an index property or some other unique identifier.

BTW, how do you post the code window in your message?
 

If you don't mind my 2 cents worth: Take a look at the Collection object. It may be all you need. I don't know whether it is supported by VB or not.

To post stuff inside a "code" box, bracket it with
Code:
 and
TGLM tags (follow the link "Process TGML" to learn more.

 
>What I thought is that if two pointers point to the same object, they would both contain the same address and could be compared on that basis

Absolutely

You need to look at the undocumented, hidden VBA function 'ObjPtr'.

Code:
[COLOR=blue]Public Sub Example()
    Dim a As Collection
    Dim b As Collection
    
    Set a = New Collection
    Set b = a
    
    Debug.Print ObjPtr(a), ObjPtr(b)
    
End Sub[/color]
 
It looks like strongm's suggestion is what i was looking for! Thank you!

I got it working after I fixed the other bugs in the code!
 
Did you figure out how to do the code window thing?

Gerry
 
Hi jm314,

I would rather use object comparison operator in your code instead of "=":

Code:
...
Do Until CurrentRecord [b]Is[/b] LastRecord
...

combo
 
That's the way to do it, combo => *

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top