Inside Out

For Each objAssign In ActiveProject.Tasks(1).Assignments

MsgBox objAssign.ResourceName Next objAssign

Set objAssign = Nothing

Tip Object variables that are local in scope will be automatically cleared when the procedure ends and they go out of scope.

• When making multiple calls to an object, use the With and End With statements.

This code makes the Office Assistant feature visible and causes it to perform an animation action:

Assistant.Visible = True Assistant.Sounds = True

Assistant.Animation = msoAnimationGetAttentionMajor

As you can see, there are three calls to the Assistant object. Each time Visual Basic encounters one of those calls, it has to create another internal reference to the Assistant object. The following construction performs the same action and is much more readable, and Visual Basic has to create only one internal object reference:

With Assistant

.Visible = True .Sounds = True

.Animation = msoAnimationGetAttentionMajor End With

• Always close connections to external objects.

When working with connections to data sources or other applications, you should always be sure to close the connection when you are finished with it. The reason for this becomes obvious when working with other applications programmatically. When you use Automation to access the Microsoft Excel object model from Microsoft Project, for example, you are actually starting Excel invisibly. If you fail to close the connection when you are done, Excel continues to run—and take up memory—until the computer is shut down.

For more information about Automation and working with other applications programmatically, see Chapter 31, "Writing Microsoft Project Code with Visual Basic for Applications."

• Increase the upper bound of a dynamic array in chunks.

When you redimension the upper bound of a dynamic array, avoid doing so individually for each new element. Instead, make a reasonable guess of how many additional elements are likely to be necessary and enlarge the array accordingly.

Tip Track your elements

To know when you're running out of available elements from the last time the array was redimensioned, you'll need to create a variable that tracks how many elements are already in use. If you don't, and you assign a value to an element that hasn't been created, you'll receive a "subscript out of range" error message.

• When determining which data type to use, choose the best one for the job.

As discussed earlier in this chapter, in a general sense, using a specific data type is faster and almost always uses less memory than when using a Variant. More specifically, there are several different numeric data types, each suitable for a different range of values, and each using more or less memory than the others.

Note You need to be aware of the individual limitations of each numeric data type when determining which type to use. For example, assigning a value greater than 32,767 to an Integer or assigning any negative value to a byte causes an overflow error.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment