Quantcast
Channel: VBForums - ASP, VB Script
Viewing all articles
Browse latest Browse all 688

Question about array type parameters in VBScript

$
0
0
Using the MSScriptControl in VB6 as the host, I noticed some interesting things about passing parameters to a VBScript function. My The code on the host side is:
Code:

Private Sub Command1_Click()
    Dim Data(1, 1) As Variant
   
    Data(0, 0) = 1
    Data(1, 0) = &H7FFFFFFF
    Data(0, 1) = 1.23
    Data(1, 1) = "a"
   
    SC1.CodeObject.ProcessData Data()
End Sub

This way the script will be executed when I click the button (the script itself is loaded in the Load event in Form1, but I haven't posted that code here, for the sake of simplicity).

And the code in the script is:
Code:

Public Sub ProcessData(ByVal Data())
        MsgBox Data(0,0)
        MsgBox Data(1,0)
        MsgBox Data(0,1)
        MsgBox Data(1,1)
End Sub

After I wrote this script, and ran it, and saw it worked, I noticed an error in the code that should have prevented it from working. In that first line, it says "ByVal Data()" for the first parameter. ByVal though is NOT valid for an array. Only ByRef is valid. But it still worked without giving any errors. And not only did it run, it behaved exactly as expected, as if I had correctly used ByRef, even though it said ByVal instead. I did later correct it to ByRef, and of course that also worked.

Can someone here explain why both of these worked?

Viewing all articles
Browse latest Browse all 688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>