3

To quote my colleague:

all variables in VBA are variants but with different vartype:

Dim x      '--->variant with vartype = vbEmpty  
x= "hello" '--->vartype changed from vbEmpty to vbString and value assigned  
x= 1       '--->vartype  changed to vbInteger

Dim x as String  '--->variant with vartype = vbEmpty is created and then vartype changed to vbString  
x= "hello"       '--->vartype = vbString  
x=1              '--->because x was declared explicitly with String it will try to implicitly convert 1 to string, so, x will remain vbString

My main point of contention is how can Dim x as String fit in with his statement that all variables in VBA are variants. Variants are more inefficient to work with so why would everything start there and get converted.

Are all variables, in VBA, variant?
Can anyone find any documentation or provide code that categorically proves the answer one way or the other?


EDIT

He put forward the following as a start to try and prove the above - but I think that all it proves is that they are all strings:

Sub aaa()

Dim str_str As String
Dim str_var

str_str = "aaa"
str_var = "aaa"
str_xxx = "aaa"

MsgBox VarType(str_str) & ": " & TypeName(str_str)
MsgBox VarType(str_var) & ": " & TypeName(str_var)
MsgBox VarType(str_xxx) & ": " & TypeName(str_xxx)

End Sub
whytheq
  • 34,466
  • 65
  • 172
  • 267
  • [**`this`**](http://msdn.microsoft.com/en-us/library/office/gg251448.aspx) should really be enough :) –  Oct 17 '13 at 08:23
  • 1
    @mehow thanks... _that_ implies that my friend is wrong but doesn't prove that he's wrong – whytheq Oct 17 '13 at 12:54
  • quot. *`The Variant data type is the data type for all variables that are not`* **`explicitly`** *`declared as some other type`* - proves him wrong. So `Dim x as String` makes it a `String` type variable not a `Variant` because it's explicitly declared as `String` –  Oct 17 '13 at 12:57
  • 1
    @mehow: Well, strictly speaking, that statement *doesn't* prove him wrong. Reordering the statement: "The data type is Variant IF the variable is not explicitly declared". Note that it says "if" and [not "if and only if"](http://en.wikipedia.org/wiki/If_and_only_if#Distinction_from_.22if.22_and_.22only_if.22) (which would indeed prove him wrong). – Jean-François Corbett Oct 18 '13 at 06:43
  • @whytheq: That `VarType` exercise doesn't shed any light on the issue: `VarType` only returns 12 i.e. `vbVariant` if the variable is an *array* of variants. So it won't return 12 for `str_var` even though `str_var` is in fact a Variant (of the Variant/String variety, more precisely). – Jean-François Corbett Oct 18 '13 at 06:46
  • @Jean-FrançoisCorbett as you mentioned - we have no way of categorically refuting the extraordinary claim that behind the scenes when `Dim s As String` is executed initially x is _initially_ variant with vartype = vbEmpty then instantly dimensioned to String type and size. It doesn't really matter though ... as far as my programs are concerned **x is a String**!! – whytheq Oct 18 '13 at 07:14
  • @whytheq: the moon may well be made of blue cheese but instantaneously change to rock when someone sets foot on it. It would be much more difficult to write a VBA interpreter the extraordinary way suggested, not to mention that it would also be much slower. In short there is no reason for the extraordinary way to be credible and all the available evidence shows it not to be true. – Charles Williams Oct 18 '13 at 07:37

3 Answers3

11

You can demonstrate using Debug and the Locals window that what your colleague says is incorrect: enter image description here
Dim as string creates a string type but Dim as Variant and assigning a string gives you a variant with a sub-type of string

Charles Williams
  • 23,121
  • 5
  • 38
  • 38
7

No, not all variables are Variant. Variants and Strings have different storage sizes; this memory gets allocated depending on the data type in the Dim statement. One doesn't just magically turn into the other.

Look up "Data Type Summary" in the VBA help file. Or, look here: http://msdn.microsoft.com/en-us/library/aa263420%28v=vs.60%29.aspx

'--->variant with vartype = vbEmpty is created and then vartype changed to vbString

Where did your colleague get this idea?! There is absolutely no evidence for this. The documentation specifically says, "When variables are initialized [...], a variable-length string is initialized to a zero-length string ("")", not vbEmpty.

The source of his confusion may be that

Dim x

is, by default, the same as

Dim x as Variant

but he probably guessed (wrongly) that when you write Dim x As String, the Dim x gets interpreted first thus creating a Variant, then the As String comes in, turning it into a String. This is just plain incorrect. The As String is part of the Dim statement, and the whole statement gets interpreted at once.

Strictly addressing your question, I don't think it's possible to find any documentation that specifically says "Dim x As String doesn't first create a Variant then change it into a string". But really, the burden is on your colleague to produce evidence for his extraordinary claims. He can look all day long, he won't find any.

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
  • +1 thanks for the info Jean-Francois - I actually think that variant and its existence and the way `VBA` behaves when using this type is one of the most inetersting aspects of the language. – whytheq Oct 17 '13 at 07:46
  • Thanks ....my friend has already played the card "we're never going to know for sure". Could do with the email address of the lead developer of the language ?! – whytheq Oct 17 '13 at 13:07
1

For the sake of completeness (as addition to the above):

What your friend talks about is perhaps this (example at the bottom):

http://office.microsoft.com/en-001/access-help/vartype-function-HA001228932.aspx

It is a VBA function and nothing more than a useful tool in case you want to make use of dynamic typecasting.

In case you only use dynamic typing and apply the example in the link "always" (bad practise in VBA), then all your variables would start of as variants (highly unrecommended).

Perhaps he confuses VBA static typing with a programming language like Javascript, which doesn't use static typing; JS make implicit type conversions: var i = 0 ~> implicitly typed as a number by the JS runtime.

Note: while static typing is recommended in VBA, there are in fact cases when VBA programmers use dynamic typing (in case the type of the variable is not known before runtime).

html_programmer
  • 18,126
  • 18
  • 85
  • 158
  • ...no need to go as far as JavaScript: just need to look to VBScript for all variant variables. – whytheq Oct 17 '13 at 16:44
  • Think he's arguing the the initial memory allocated to `Dim x as String` is variant size then it gets dimensioned to `String` size...but unless proved then general knowledge is that a **String is a String** !! – whytheq Oct 17 '13 at 16:47
  • Of course, there's no doubt about that. I didn't know about VBscript, I only write Javascript. – html_programmer Oct 17 '13 at 17:31