Named Arguments

When supplying arguments for a method, each argument is separated from the next with a comma; for example:

ActiveProject.Tasks.Add "Write chapter", 3

When it comes to self-documenting code, however, that code isn't especially descriptive. You can probably guess that "Write chapter" is the name of the new task, but it's harder to guess what the 3 is for. This is one way in which named arguments can be valuable.

Using named arguments means that instead of supplying just the values for each argument, you precede each value with the name of the argument. The argument name is then separated from the argument value with a colon and an equal sign:

ActiveProject.Tasks.Add Name:="Write chapter", Before:=3

It is now clear that the name of the new task is "Write chapter" and that it appears before the task that is currently third in the task list.

Named arguments are also used for methods that have many optional arguments, some of which you aren't using. Without named arguments, you must supply a comma as a placeholder for each optional argument, so that the arguments you are using appear in the expected order and position. This results in code that can be quite cryptic:

Application.WBSCodeMaskEdit , , , 2, "-"

The only way to find out what the two values are being used for is to look up the method in Help. If you use named arguments, however, you don't need to use placeholder commas and the code is very descriptive:

Application.WBSCodeMaskEdit Length:=2, separator:="-"

When you use named arguments, you can specify them in any order you choose. The code in the previous example could have been written with the two arguments in reverse order:

Application.WBSCodeMaskEdit separator:=-, Length:=2Constants

Constants are an important and useful feature of Visual Basic that makes code self-documenting. In fact, Visual Basic is full of constants, including what is probably the most common constant of all: vbCrLf. This constant, which represents a carriage-return and linefeed, is used to format text for display. The following code displays a sentence in two lines:

MsgBox "Here is a long sentence, " & vbCrLf & "broken into two lines."

The vbCrLf constant is much more user-friendly, not to mention convenient, than the old method of using Chr$(13) & Chr$(10) to do the same thing.

As mentioned earlier in this chapter, constants are frequently used to represent fixed numeric data, such as the Microsoft Project constant "pjFinishToStart." The name of the constant, especially when you know that it's a member of the PjTaskLinkType enumeration ("enum"), is very effective at describing what kind of link is being applied with the LinkPredecessors method, for example. Using just the numeric value of pjFinishToStart doesn't provide you with any real information at all.

You can create your own constants much as you would a variable, except you use the Const keyword and immediately assign a value to it:

Const c_lngMyFinishToStart As Long = 10

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