8

When I set a variable to an object in VBA / VBScript, I always need to prefix the statement with "Set" such as: Set Dict = CreateObject("Scripting.Dictionary")

If I set a variable to a primitive or array value, I have to omit the "Set" such as: Num = 123

This is different from languages such as JavaScript, PHP, Python etc. Is there some technical reason why they have this requirement in VBA / VBScript?

2
  • 1
  • COM objects are not native to VB, its a case of a non OO language interfacing with an OO API, rather than re-write the whole of basic from scratch they tacked on the "Set" keyword to allow VB to attach to COM objects. May 21, 2014 at 8:22

2 Answers 2

12

VB and VBA had something called "default properties". If your code mentioned the object without any property/ method specified, then you got the default property, not the object!

Dim vValue as Variant

vValue = textBox1

? TypeName( vValue )

String

Should this work? Well, yes, because VB interprets this as ...

vValue = textBox1.Text

... because Text is the textbox's default property.

The Set statement overrides all that, forcing VB to use the object itself.

Set vValue = textBox1

? TypeName( vValue )

TextBox

5
  • 2
    While this is an interesting side effect of the Set semantics, it's not really an answer to the question. May 21, 2014 at 11:47
  • 3
    @AvnerShahar-Kashtan - I'd say it's an interesting side effect of not using Set.
    – JeffO
    May 21, 2014 at 11:59
  • Should "sValue" above be "vValue"? If so, then I think this answers the original question very well. If not, then I don't understand this answer at all as nothing is ever assigned to vValue.
    – OldFart
    May 21, 2014 at 21:18
  • 5
    @AvnerShahar-Kashtan: It's not a side-effect - it's a requirement of the language. If you want to work with an object reference then you MUST use the Set statement. If you're happy to work with every object's default property, then you can leave it out. Omitting default properties from Visual Basic.Net was, IMHO, one of the better things Our Friends in Redmond did with the language (sadly, I believe they've reappeared in more recent versions).
    – Phill W.
    May 22, 2014 at 14:26
  • 5
    This does answer the original question. The only reason for Set is to avoid ambiguity. Without Set, you wouldn't know whether you're assigning an object reference or the object's default property. In VB .NET this is avoided because there are no default properties (except for indexed properties and/or iterators, but those avoid the ambiguity due to their different syntax).
    – Kyralessa
    May 22, 2014 at 16:13
1

Note: when I say "Visual Basic" here I mean Visual Basic 6 and before - not Visual Basic.NET.

Visual Basic is designed to be easy to use. Pointers are hard - and to some extent, so are reference types, like the ones used for objects in most modern languages.

the Set syntax is a way to hide this:

Set foo = Bar() 'Treat foo as reference variable
foo = Bar() 'Treat foo as a value variable

So, people that don't understand references can use objects like value variables, which are easier to grasp. Given that Visual Basic is usually used with the Visual Studio form designer to create GUI applications, you can do quite a lot without using references.

VBA and VBS usually require more reference usage(in VBA you usually need to use Office objects, and in VBS you usually need to use COM objects to interact with Windows), but their syntax still needs to follow the Visual Basic one so they also require the Set syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.