Reputation: 35557
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
Upvotes: 3
Views: 1242
Reputation: 38500
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.
Upvotes: 7
Reputation: 18859
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).
Upvotes: 1
Reputation: 23505
You can demonstrate using Debug and the Locals window that what your colleague says is incorrect:
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
Upvotes: 11