Although it is simple, but very easy to be missed, and hard to find the root problem in run time. You try to write a common routine and it behaves differently depending on the values of the parameters passed in. For example, (see code in the section below) you have a routine DoSomethingWithParentDoc, it takes 3 parameters: parentDoc, childDoc, and db. In the first case, you have the parentDoc to pass in, so it will operate on the parentDoc directly; in the second case, you have the childDoc to pass in, and you need to get the parentDoc in the common routine and then operate on the parentDoc. See what you can/cannot do in the coding example below.
'************** calling routine
Sub Initialize
Dim docUNID As String
Dim answer As Integer
Dim session As New NotesSession
Dim db As NotesDatabase
Dim parentDoc As NotesDocument
Dim childrenDoc As NotesDocument
Set db = session.CurrentDatabase
answer = Inputbox("Enter 1 if you have parentDoc UNID, 2 if you have childrenDoc UNID.")
'Call the same routine but pass in different values for each parameter
If answer = 1 Then
'This is the case we have the parentDoc
docUNID = Inputbox("Enter the parentDoc UNID:")
Set parentDoc = db.GetDocumentByUNID(docUNID)
'Do not need childDoc, so pass in Nothing
Call DoSomethingWithParentDoc(parentDoc, Nothing, db)
Elseif answer = 2 Then
'This is the case we have the childrenDoc
docUNID = Inputbox("Enter the childrenDoc UNID:")
Set childrenDoc = db.GetDocumentByUNID(docUNID)
'Do not have parentDoc, so pass in Nothing
Call DoSomethingWithParentDoc(Nothing, childrenDoc, db)
End If
End Sub
'********************** Incorrect common routine example
Sub DoSomethingWithParentDoc(parentDoc As NotesDocument, childrenDoc
As NotesDocument, db As NotesDatabase)
'Incorrect set of parentDoc
If parentDoc Is Nothing Then
Set parentDoc = db.GetDocumentByUNID(childrenDoc.ParentDocumentUNID)
'Nothing's wrong from compiling perspective, but parentDoc will
never be set as it was passed in as Nothing, which is a constant,
constant cannot be reset.
End If
If parentDoc Is Nothing Then
'In this incorrect parentDoc setting case, we will always reach here
Msgbox ("The passed in parentDoc could not be reset, it is still Nothing!")
Exit Sub
Elseif parentDoc.IsValid Then
Msgbox ("The passed in parentDoc was successfully set to the Parent
Document of the passed in childrenDoc.")
End If
'Do something with the parentDoc .....
End Sub
'********************** Correct common routine example
Sub DoSomethingWithParentDoc(inParentDoc As NotesDocument, childrenDoc
As NotesDocument, db As NotesDatabase)
Dim parentDoc As NotesDocument
'Correct set of parentDoc
If inParentDoc Is Nothing Then
Set parentDoc = db.GetDocumentByUNID(childrenDoc.ParentDocumentUNID)
'Here parentDoc is a local variable, if the inParentDoc is Nothing
(remember this is a constant, cannot be reset), we set the local parentDoc
Else
Set parentDoc = inParentDoc
'The inParentDoc is not Nothing, this is the case we got the
parentDoc, assign it to the local parentDoc variable as the local
parentDoc variable is the one to be used for later operations
End If
If parentDoc Is Nothing Then
Msgbox ("The passed in parentDoc could not be reset, it is still Nothing!")
Exit Sub
Elseif parentDoc.IsValid Then
'In this case, the parentDoc has been correctly set
Msgbox ("The passed in parentDoc was successfully set to the Parent
Document of the passed in childrenDoc.")
End If
'Do something with the parentDoc .....
End Sub
This was first published in June 2002