1

The VBA syntax for the add method of the dictionary class is given as:

Dictionary.Add (Key as String, Item as Variant) 

But actually including these parentheses generates a syntax error.

So if D is an object of type dictionary, then vba expects: D.Add "key1", "value1"

and not:

D.Add("key1", "value1") <= this generates an error!

Contrasting the Add method with Exists:

Dictionary.Exists (Key as String)

The parentheses are actually expected:

V1 = D.Exists("key1")

So why is it that the Add syntax specifies ()'s, but doesn't actually expect them (and even generates an error if they are used), while the Exists syntax specifies them and does actually expect them?

tgoneil
  • 1,522
  • 3
  • 19
  • 30
  • I think VBA uses the same rules as VBScript. See this: http://stackoverflow.com/questions/5413765/what-are-the-rules-governing-usage-of-brackets-in-vba-function-calls – HK1 Oct 01 '13 at 21:33
  • The example you just gave says not to use ()'s if no arguments to a sub, but to use ()'s if there are arguments. In the Add example above, there are arguments key1, value1. So ()'s should be expected according to that example, right? – tgoneil Oct 01 '13 at 21:37

1 Answers1

1

In VBA, if you call a Sub with parameters, you can call it either with:

YourSub Parameter1

or

Call YourSub(Parameter1)

Note that YourSub (Parameter1) will actually cast Parameter1 to a string and then hand it over to YourSub - which is most certainly not what you want!!!

In case it is a Function, you would call it:

result = YourFunction(Parameter1)

In case of a Dictionary object, .Add is a method/sub, i.e. you use dict.Add Key, Value, while .Exists is a function, so you need to use if dict.Exist(Key)...

Peter Albert
  • 16,917
  • 5
  • 64
  • 88
  • Thanks for clarifying subs vs functions. That seems to answer the question. I'll mark as answered in 5 minutes when it will let me. Silly time delay. :-) – tgoneil Oct 01 '13 at 21:41
  • So, why doesn't the syntax specify: Dictionary.Add Key, Value telling the programmer not to use ()'s instead of Dictionary.Add (Key, Value)? – tgoneil Oct 01 '13 at 21:43
  • 1
    My favorite explanation is this [Daily Dose of Excel post](http://dailydoseofexcel.com/archives/2012/05/01/quick-vba-tip-parentheses/). – Doug Glancy Oct 01 '13 at 21:45
  • Thanks for the reference Doug. – tgoneil Oct 01 '13 at 21:46
  • @tgoneil: I don't know. Which "syntax" are you referring to exactly? – Peter Albert Oct 01 '13 at 21:48
  • As noted initially, Dictionary.Add (Key as String, Item as Variant) – tgoneil Oct 01 '13 at 21:51
  • i meant, where exactly do you see this syntax? in the help file/MSDN/etc.? – Peter Albert Oct 01 '13 at 21:59
  • 1
    @PeterAlbert: Apparently the motobit reference is wrong. MSDN's version has no ()'s for add -- http://msdn.microsoft.com/en-us/library/aa265006%28v=vs.60%29.aspx Wished I'd seen this reference in the first place. Would have saved me from starting this entire conversation. – tgoneil Oct 02 '13 at 03:35