The Set Statement Goes Away

One of the many areas in which Visual Basic could be confusing to newcomers was in its use of the Set keyword. For example, if you wanted to create an instance of an ActiveX control with the ProgID Foo.Bar, you would use code such as the following:

Dim foo As Foo.Bar

Set foo = New Foo.Bar

Creating an object requires using the Set keyword. Unfortunately, many developers don't have a good understanding of what exactly is and is not an "object" from the Visual Basic point of view; I've seen more than a few programmers who play with using or not using the Set keyword in a vain attempt to get their programs going. Sometimes the result is a working program, because the presence or absence of the Set keyword was the problem, but as often as not the real problem remains hidden until you look at the code more closely.

Why was the Set keyword ever used? In Visual Basic 6.0 and earlier, objects had default properties that didn't require a parameter. So if an object foo had a parameterless default property called bar, without using Set there was the chance for ambiguity, as in the following example:

Dim f as foo

Dim o as Object foo=o

In this case, it's unclear whether foo.bar should be set to o or whether foo should be set to o. Visual Basic .NET eliminates the need for using Set by eliminating parameterless default properties. More than eliminating the need for Set, in Visual Basic .NET, the Set keyword is no longer allowed.

0 0

Post a comment