Tip

The value of a variable passed in as constant cannot be changed

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.