Improving Application Performance

Improving Application Performance

This section describes how your Microsoft® Visual Basic® code can operate most efficiently when you use CDO Library objects. Note that this section is written primarily for Visual Basic programmers rather than for C programmers.

To access CDO Library objects, you create Visual Basic statements that concatenate the object names in sequence from left to right, separating objects with the period character. For example, consider the following Visual Basic statement:

Set objMessage = objSession.Inbox.Messages.GetFirst
 

The CDO Library creates an internal object for each period that appears in the statement. For example, the portion of the statement that says objSession.Inbox directs the CDO Library to create an internal Folder object that represents the user’s Inbox. The next portion, .Messages, directs the CDO Library to create an internal Messages collection object. The final part, .GetFirst, directs the CDO Library to create an internal Message object that represents the first message in the user’s Inbox. The statement contains three periods; the CDO Library creates three internal objects.

The best rule of thumb is to remember that periods are expensive. For example, the following two lines of code are very inefficient:

' warning: do not code this way; this is inefficient
MsgBox "Text: " & objSession.Inbox.Messages.GetFirst.Text
MsgBox "Subj: " & objSession.Inbox.Messages.GetFirst.Subject
 

While this code generates correct results, it is not efficient. For the first statement, the CDO Library creates internal objects that represent the Inbox, its Messages collection, and its first message. After the application displays the text, these internal objects are discarded. In the next line, the same internal objects are generated again. A more efficient approach is to generate the internal objects only once:

With objSession.Inbox.Messages.GetFirst
    MsgBox "Text: " & .Text
    MsgBox "Subj: " & .Subject
End With
 

When your application needs to use an object more than once, define a variable for the object and set its value. The following code fragment is very efficient when your application reuses the Folder or Message objects or the Messages collection:

' efficient when the objects are reused
Set objInboxFolder = objSession.Inbox
Set objInMessages = objInboxFolder.Messages
Set objOneMessage = objInMessages.GetFirst
With objOneMessage
    MsgBox "The Message Text: " & .Text
    MsgBox "The Message Subject: " & .Subject
End With
 

Now that you understand that a period in a statement directs the CDO Library to create a new internal object, you can see that the following two lines of code are not only not optimal but actually incorrect:

' error: collection returns the same message both times
MsgBox("first message: " & inBoxObj.Messages.GetFirst)
MsgBox("next message: " & inBoxObj.Messages.GetNext)
 

The CDO Library creates a temporary internal object that represents the Messages collection, then discards it after displaying the first message. The second statement directs the CDO Library to create another new temporary object that represents the Messages collection. This Messages collection is new and has no state information, that is, this new collection has not called GetFirst. The GetNext statement therefore causes it to return its first message again.

Use the Visual Basic With statement or explicit variables to generate the expected results. The following code fragment shows both approaches:

' Use of the Visual Basic With statement
With objSession.Inbox.Messages
    Set objMessage = .GetFirst
    ' ...
    Set objMessage = .GetNext
End With
' Use of explicit variables to refer to the collection
Set objMsgColl = objSession.Inbox.Messages
Set objMessage = myMsgColl.GetFirst
...
Set objMessage = myMsgColl.GetNext
 

For more information about improving the performance of your applications, see your Visual Basic programming documentation.

See Also

Concepts

Programming Tasks