VBScript
Local
External
Variant Array Building in C++
void loadStringList( VARIANT *vList, char **strs, int numStr)
{
// DebugBreak();
VariantInit(vList);
LPSAFEARRAY psa;
SAFEARRAYBOUND rgsabound[] = { numStr, 0 };
psa = SafeArrayCreate(VT_VARIANT, 1, rgsabound);
V_VT(vList) = VT_ARRAY | VT_VARIANT;
V_ARRAY(vList) = psa;
LPVARIANT rgElems;
SafeArrayAccessData(psa, (LPVOID*)&rgElems);
for( int i = 0; i < numStr; i++) {
CComBSTR cbs( strs[i]);
rgElems[i].vt = VT_BSTR;
rgElems[i].bstrVal = SysAllocString( cbs);
}
SafeArrayUnaccessData(psa);
}
Displaying the contents of an Array
<script language="vbscript">
<!--
option explicit
Sub DisplayElements(pvaBstr)
Alert "TypeName(pvaBstr) = " & TypeName(pvaBstr)
if Not IsArray(pvaBstr) then
Alert("Array expected!")
Exit Sub
end if
dim nLBound
dim nUBound
nLBound = LBound(pvaBstr)
nUBound = UBound(pvaBstr)
Alert "There are " & nUBound - nLBound + 1 & " elements in this array."
' Get the type of this array
dim cTypeName
cTypeName = TypeName(pvaBstr)
' Because it's an array VBSCRIPT appends "()" on the end. Remove it.
cTypeName = UCase(Left(cTypeName, Len(cTypeName)-2))
if cTypeName = "VARIANT" then
Alert("VBScript likes arrays of type VARIANT.")
else
Alert("VBScript can't handle arrays of raw type " & cTypeName _
& ", but here goes...")
end if
dim i
for i = nLBound to nUBound
Alert ("pvaBstr(" & i & ")=" & pvaBstr(i))
next
End Sub
-->
</script>
Debug
Debug Output to a TextArea
<form name="form1" id="Form1">
<textarea id=debugText rows=8 name="debugText">
</textarea>
</form>
<script language="vbscript">
<!--
option explicit
sub Test5b2
document.form1.debugText.value = document.form1.debugText.value + "new text" + VBNewLine
end sub
-->
</script>
Calling Code in Another Frame
How to call code
<script language="vbscript">
<!--
sub func()
'###### Lots of ways to call a sub in another frame
parent.otherFrameName.subName()
top.otherFrameName.subName()
call parent.otherFrameName.subName()
call top.otherFrameName.subName()
'###### Lots of ways to control an ActiveX control in another frame
parent.otherFrameName.ActiveXcontrolID.methodName()
top.otherFrameName.ActiveXcontrolID.methodName()
call parent.otherFrameName.ActiveXcontrolID.methodName()
call top.otherFrameName.ActiveXcontrolID.methodName()
end sub
-->
</script>
Parent Frame's HTML
<html>
<head>
<title>Title Goes Here</title>
<script>
</script>
</head>
<frameset cols="15%,*" id="top">
<frame name="Controls" src="./ControlPanel.html">
<frame name="ActiveX" src="./SceneGraphControl.html">
</frameset>
</html>
Gotchas
- Comments start with ' (single quote)
dim You cannot delare a variable with 'dim' and assign it at the same time
- Line continuation, to make a line of code span more than 1 line, use _ (underscore) as the line continuation character
Strings
' Concatination
msgbox( "This shows " & "string " & "concatination " & 3 & " and " & 4)
' New line
msgbox( "line 1 " & VBCR & "line 2")
Arrays
<script language="VBScript">
<!--
option explicit
' Dynamic Array
dim dynamicArray()
' Fixed size Array
dim fixedArray(5)
' variable with _3_ ! yes 3 elements:
dim names(2)
names(0)="first"
names(1)="second"
names(2)="third"
' Create an array of a few elements
dim array1
array1 = array( "a", "b", "c")
' erase empties an array or ?frees a dynamic Array
dim array2()
ReDim array2(5)
erase array2
' ReDim and preserve original data
ReDim Preserve array2
' looping
Dim arrayLooped
Dim element
arrayLooped = array( 1, 2, 3)
for each element in arrayLooped
msgbox("An element is " + element)
next
Dim i
for i = LBound(arrayLooped) to UBound(arrayLooped)
msgbox("An element is " + arrayLooped(i))
next
' Getting the size of a dimension
Dim array(5)
Dim size
size = UBound(array)
Dim array( 3, 4)
Dim size
size = UBound( array, 1)
size = UBound( array, 2)
' LBound is also valid (maybe if COM made the VARIANT with a LBound set???)
' Split to parse a string into an array
Dim arrParsed
arrParsed = split("alpha,beta,gamma", ",")
arrParsed = split("alpha+beta+gamma", "+")
' Join array of strings into one string
Dim strTogether
strTogether = join( arrParsed, ",")
' Filter
arrOut = Filter( arrayIn, strSearch [, flagInclude = true [, nCompareType = ?? 0 ]])
' flagInclude
' true = returns strings that include strSearch
' false = returns strings that exclude strSearch
' nCompareType
' 0 = use a binary compare
' 1 = use a text compare
'
-->
</script>
Coding Standards
<script language="VBScript">
<!--
' Use this at the top of the code to force all variables to be declared b4 used:
option explicit
-->
</script>