Why does my object dissapear?

I have created a class to expose certain elements of contact documents in a contact databse. The task seemed straightforward enough, get a handle to the database, a lookup view, pass in a lookup key, get a handle to the associated document in that view, then return the values desired from that document.

Since I don’t know which elements will be needed at any given time, and since it seemed both tedious and wasteful to write every value I want to expose into memory every time I create this object, I figured I would store the handle to the document in a class level variable and then ask for the approprate item value for each value asked for. A simplified version of this object (which I have also tested) is shown below:

Public Class Client

Private mClientDoc As NotesDocument



Public Sub New(strClientId As String)

	Dim db As NotesDatabase

	Dim lookupView As NotesView

	Set db = New NotesDatabase("", "")

	If Not (db.OpenByReplicaID("", CLIENT_DB_REPLICA_ID) ) Then

		Error 1115, "Client database not found."

	Else

		Set lookupView = db.GetView(CLIENT_LOOKUP_VIEW)

		Set mClientDoc = lookupView.GetDocumentByKey(strClientId, True)

		Print mClientDoc.GetItemValue("ClientName")(0) ' This succeeds

	End If

End Sub



Public Property Get clientName As String

	clientName = mClientDoc.GetItemValue("ClientName")(0) ' This fails claiming error 91 object not set!

End Property

End Class

As you can see by the comments, I’ve got a handle to the document upon creation and can access its item values but when I later ask for the document’s ClientName value, the document handle is gone! I even modified the property to verify it was, indeed the mClientDoc that is not set:

Public Property Get clientName As String

	If (mClientDoc Is Nothing) Then

		Print "What happened to my doc?"

	Else

		clientName = mClientDoc.GetItemValue("ClientName")(0)

	End If

End Property

Sure enough, I get the print statement because mClientDoc is somehow no longer set! Am I missing something obvious? Why am I unable to maintain my pointer to this object?

Subject: Why does my object dissapear?

Notice mClientDoc is contained by your NotesDatabase db. What is the scope of db? When db goes out of scope whatever it contains goes away with it.

Subject: RE: Why does my object dissapear?

Doh! I was, indeed, missing something obvious. Thanks. :slight_smile:

Subject: RE: Why does my object dissapear?

Glad to help!