Creating and Referencing Multidimensional Arrays
I'm just confused because I'm obviously calling the code wrong but every tutorial I find makes it seem like I'm referencing the multidimensional arrays correctly so I don't knwo what I'm doing wrong. Can anyone help me out please??
aProgList(0) = "DRA, Directory and Resource Administrator, C:\Program Files\NetIQ\DRA\UserConsole.exe"
aProgList(1) = "Remedy, Action Request System - Remedy User, C:\Program Files\AR System\User\aruser.exe"
x = 0
For Each str In aProgList
ReDim Preserve aExpProg(x)
aExpProg(x) = Split(str,", ")
x = x + 1
Next
' Attempt 1
For Each item In aExpProg
WScript.Echo item(0)
WScript.Echo item(1)
WScript.Echo item(2)
Next
' Attempt 2
'WScript.Echo aExpProg(0,0)
'WScript.Echo aExpProg(0,1)
'WScript.Echo aExpProg(0,2)
Attempt 1 works great which tells me that my multidimensional array is working the way I want it to be but Attempt 2 tells me:
Microsoft VBScript runtime error: Subscript out of range
...and I can't figure out why? Any pointers?
[1166 byte] By [
thepip3r] at [2007-11-11 9:52:12]

# 1 Re: Creating and Referencing Multidimensional Arrays
You're not creating a multidimensional array, you're creating a single-dimension string array with comma-delimited items. The number of dimensions is determined by how many parameters you pass to the Dim statement. You're passing one parameter:
Dim aProgList(1)
so that means you have a one-dimension array. I think this is what you're trying to do:
Dim aProgList(1, 1)
aProgList(0, 0) = "DRA, Directory and Resource Administrator"
aProgList(0, 1) = "C:\Program Files\NetIQ\DRA\UserConsole.exe"
aProgList(1, 0) = "Remedy, Action Request System - Remedy User"
aProgList(1, 1) = "C:\Program Files\AR System\User\aruser.exe"
# 2 Re: Creating and Referencing Multidimensional Arrays
What you have actually created a an array of variant's where each element of the array contains another array of strings so to retreive the data stored in the array you would need nested loops the first to reference the variant array and the other to reference the string array stored in the element you are currently referencing. Here is a little test sub you can experiment with:
Sub ArrayTest()
Dim aProgList() As Variant
Dim I As Long
Dim J As Long
For I = 0 To 3
ReDim Preserve aProgList(I)
'These two lines of code do the exact same thing
'they create an array of strings an store that array into
'array element of aProgList(I).
'aProgList(I) = Array("Fred", "Sam", "Henry", "David", "Joe")
'aProgList(I) = Split("Fred,Sam,Henry,David,Joe", ",")
aProgList(I) = Split("Fred,Sam,Henry,David,Joe", ",")
Next I
'Here is how you get the data back out
For I = LBound(aProgList) To UBound(aProgList)
For J = LBound(aProgList(I)) To UBound(aProgList(I))
'aProgList(I) contains an array so to get to the values
'in that array you must reference both the aProgList Element...(I)
'and the element of the array stored within it...(J)
Debug.Print I & "," & J & " -> " & aProgList(I)(J)
Next J
Next I
Erase aProgList
End Sub
# 3 Re: Creating and Referencing Multidimensional Arrays
Hi, Ron: Just to clarify, here is thepip3r's original code:
aProgList(0) = "DRA, Directory and Resource Administrator, C:\Program Files\NetIQ\DRA\UserConsole.exe"
aProgList(1) = "Remedy, Action Request System - Remedy User, C:\Program Files\AR System\User\aruser.exe"
I don't think that's creating a string array in each element. It's simply assigning a string to each element, no?
# 4 Re: Creating and Referencing Multidimensional Arrays
Actually it is, because of the use of the Split statement.
ReDim Preserve aExpProg(x)
aExpProg(x) = Split(str,", ")
# 5 Re: Creating and Referencing Multidimensional Arrays
OK, we're talking about two different arrays. :-) I'm talking about aProgList, in which he simply assigned a string to each element. You're talking about aExpProg, in which he used the Split function to assign each element. I just want to be clear so we don't confuse him more than he already is. ;-)
# 6 Re: Creating and Referencing Multidimensional Arrays
Yes, I probably should have used the aExpProg array in my example instead of the aProgList, since he was referencing aExpProg in the rest of his code.
# 7 Re: Creating and Referencing Multidimensional Arrays
Ok, I guess I need to clarify a little... I'm basically trying to create a "RunAs" script so my fellow administrators don't have to right click > runas > enter credentials on everything they want to run; especially the pains in the butt like IE. I'm also trying to do it in a simple manner for those who don't understand scripting very well; that's the reason for the first array. The individual techs only have to add the next element to the array in order to make an individualized addition to the script.
Phil: In response to your very first post: I want aProgList to be a one-dimensional array - it's aExpProg that I want to be multidimensional but I don't know how many dimensions (it will depend on how many programs are listed in aProgList); I probably just can't make it as easy as I want it to be can I?? ...Why can't all languages be as easy or have as much support as PHP =P
Option Explicit
Dim sUsername, sDomain, sProgPath, sSelectForm
Dim aProgList(1), aExpProg(), test
Dim x, str, item
sDomain = "myDomain"
sUsername = "myUserName"
aProgList(0) = "DRA, Directory and Resource Administrator, C:\Program Files\NetIQ\DRA\UserConsole.exe"
aProgList(1) = "Remedy, Action Request System - Remedy User, C:\Program Files\AR System\User\aruser.exe"
x = 0
For Each str In aProgList
ReDim Preserve aExpProg(x)
aExpProg(x) = Split(str,", ")
x = x + 1
Next
For Each item In aExpProg
WScript.Echo item(0)
WScript.Echo item(1)
WScript.Echo item(2)
Next
WScript.Echo aExpProg(0,0)
'WScript.Echo aExpProg(0,1)
'WScript.Echo aExpProg(0,2)
sSelectForm = InputBox( _
"Type the number or abbreviated name into input box to run: " & VbCrLf & VbCrLf _
& "")
If (sSelectForm = "") Or (sSelectForm = vbCancel) Then
WScript.Quit
Else
[Enter RunAs Function]
End If
Function RunProgAs (sDomain, sUsername, sProgPath)
Dim oShell
Dim sCmdLine
Set oShell = CreateObject("Wscript.Shell")
If (sDomain = "") Or (sUsername = "") Or (sProgPath = "") Then
If (sDomain = "") Then WScript.Echo "Domain is blank."
If (sUsername = "") Then WScript.Echo "Username is blank."
If (sProgPath = "") Then WScript.Echo "Program Path is blank."
WScript.Quit
End If
sCmdLine = "runas /user:" & sDomain & "\" & sUsername & " " & sProgPath
oShell.Run sCmdLine, 9, False
End Function
