excel - What is the difference between `Array` and `Array()`? -


i declare array thus:

dim arrtest() variant 

or thus:

dim arrtest2 variant arrtest2 = array() 

however, first can passed argument this:

sub(byref arrtest() variant) 

and second this:

sub(byref arrtest2 variant) 

you can second:

redim arrtest2(ubound(arrtest2) + n) 

but not first.

what difference between variable array declared first way, , variable array declared second?
they're both same vartype() - 8204 - array of variants
why macros treat them differently?

the first array of variants. always array -- can't reassigned e.g. range. second variant can hold anything, including array (which in case). consider following code:

sub test()     dim arrtest() variant     dim arrtest2 variant     arrtest2 = array()     debug.print "arrtest " & typename(arrtest)     debug.print "arrtest2 " & typename(arrtest2) end sub 

when run it, this:

arrtest variant() arrtest2 variant() 

which strikes strange. if same type why vba treat them different?

answer -- aren't same type! typename (or vartype), when applied variant variable doesn't return variables type @ all. instead returns subtype of variable (a concept makes sense variants). clearer picture of happening -- put break point before first debug.print statement, run it, , open locals window:

enter image description here

note how type of arrtest variant() arrtest2 variant/variant(0 -1). aren't same type. arrtest array has follow vba syntax regarding array. arrtest2 isn't array @ -- simple variable (of type variant) follows vba syntax of simple (non-array) variables. fact in particular case pointing array doesn't make array variable.

if familiar c, question similar question many beginning c programmers ask difference between int , int*, in ways more mysterious here since fact functions vartype , typename implicitly dereference variant variable arguably design flaw. in way nice if typename(arrtest2) return more accurate (albeit more verbose) variant/variant() in case.


Comments

Popular posts from this blog

mysql - FireDac error 314 - but DLLs are in program directory -

python - How to read gradle build progress from it's output? -

c# - How do I debug "System.DllNotFoundException: The specified procedure could not be found"? -