Improving Application Performance
The AFM creates an internal object for each period that
appears in the statement. For example, the portion of the
statement that says objFaxSession.RootFolder.Folders
directs the AFM to create two internal objects, RootFolder
and the Folders collection object that represents the user's
main folder. The next portion, .Messages, directs the
AFM to create an internal Messages collection object. The
final part, .GetFirst, directs the AFM to create an
internal Message object that represents the first message in
the user's first folder. The statement contains four
periods; the AFM creates four 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: " & objFaxSession.RootFolder.Folders(1).Messages.GetFirst.Text
MsgBox "Subj: " & objFaxSession.RootFolder.Folders(1).Messages.GetFirst.Subject
While this code generates correct results, it is not
efficient. For the first statement, the AFM creates internal
objects that represent the first folder, 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 objFaxSession.RootFolder.Folders(1).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 objFirstFolder = objSession.RootFolder.Folders(1)
Set objMessages = objFirstFolder.Messages
Set objOneMessage = objMessages.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 AFM 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: " & objFirstFolder.Messages.GetFirst)
MsgBox("next message: " & objFirstFolder.Messages.GetNext)
The AFM creates a temporary internal object that
represents the Messages collection, then discards it after
displaying the first message. The second statement directs
the AFM 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 objFaxSession.RootFolder.Folders(1).Messages
Set objMessage = .GetFirst
' ...
Set objMessage = .GetNext
End With
' Use of explicit variables to refer to the collection
Set objMsgColl = objFaxSession.RootFolder.Folders(1).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.
back to categories
list
|