Note that there are some explanatory texts on larger screens.

plurals
  1. POVBA: What is causing this string argument passed to ParamArray to get changed to a number (that looks suspiciously like a pointer)?
    text
    copied!<p>FINAL EDIT: It does indeed appear to be a compiler bug - see the accepted answer.</p> <p>Using VBA within Excel 2007, I have the following code in 'Class1':</p> <pre><code>Option Explicit Public Function strange(dummy As String, ParamArray pa()) Debug.Print pa(LBound(pa)) End Function Public Sub not_strange(dummy As String, ParamArray pa()) Debug.Print pa(LBound(pa)) End Sub Public Function also_not_strange(ParamArray pa()) Debug.Print pa(LBound(pa)) End Function </code></pre> <p>and some mode code in a module:</p> <pre><code>Option Explicit Public Function not_strange_either(dummy As String, ParamArray pa()) Debug.Print pa(LBound(pa)) End Function Public Sub outer(v) Dim c As Class1 Set c = New Class1 Call c.strange("", v(LBound(v))) Call c.not_strange("", v(LBound(v))) Call c.also_not_strange(v(LBound(v))) Call not_strange_either("", v(LBound(v))) End Sub </code></pre> <p>If call 'outer' from the Immediate window like this:</p> <pre><code>call outer(array("a")) </code></pre> <p>I get back output that seems strange:</p> <pre><code> 102085832 a a a </code></pre> <p>It seems to matter whether the called routine is in a class module or not, whether it is a Sub or a Function, and whether or not there is an initial argument. Am I missing something about how VBA is supposed to work? Any ideas?</p> <p>The strange number changes from run to run. I say "looks suspiciously like a pointer" because if I call this:</p> <pre><code>Public Sub outer2(v) Dim c As Class1 Set c = New Class1 Dim ind As Long For ind = LBound(v) To UBound(v) Call c.strange("", v(ind)) Next ind End Sub </code></pre> <p>like so:</p> <pre><code>call outer2(array("a","b","c")) </code></pre> <p>I get back output like:</p> <pre><code> 101788312 101788328 101788344 </code></pre> <p>It's the increment by 16 that makes me suspicious, but I really don't know. Also, passing a value, say by calling:</p> <pre><code>Call c.strange("", CStr(v(ind))) </code></pre> <p>works just fine.</p> <p>EDIT: A little more info...If I assign the return value from 'c.strange' to something instead of throwing it away, I get the same behavior:</p> <pre><code>Public Sub outer3(v) Dim c As Class1 Set c = New Class1 Dim x x = c.strange("", v(LBound(v))) Call c.not_strange("", v(LBound(v))) Call c.also_not_strange(v(LBound(v))) Call not_strange_either("", v(LBound(v))) End Sub </code></pre> <p>Interestingly, if I call my test routines as above, with an argument that results from calling 'Array', the supposed-pointer value changes. However, if I call it like this:</p> <pre><code>call outer([{1,2,3}]) </code></pre> <p>I get back the same number, even if I make the call repeatedly. (The number changes if I switch to another app in Windows, like my browser.) So, now I'm intrigued that the Excel evaluator (invoked with the brackets) seemingly caches its results...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload