Re: simple 2 dimensional array problem



In addition to Steve's comments, you can only redimension the last member of
an array. ReDim Preserve CDInfo(1 To x, 2) would have to be ReDim Preserve
CDInfo(2, 1 To x). Not too that by specifying 2 as the first element, in
the absence of an Option Base statement or explicit lower bound declaration,
you are creating an array with three elements (0, 1, and 2).

--

Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/

Please reply to the newsgroups so all can participate.




"Johnno" <jstt@xxxxxxxxxxxxxxxxx> wrote in message
news:44c61c92$1_3@xxxxxxxxxxxxxxxxxxxxxx
I am having trouble working out how to make a two dimensional array work
correctly. I have tried the code below, but keep getting a Type Mismatch. If
I simply assign simple variables to the Drive and Caption items, the code
works fine and I get the results I expect. Can someone please tell me where
the code is wrong? The Dim statements and the procedure are in a module,
while the command button is on a form.
Johnno.

Dim ItemSet As Object
Dim Item As Object
Dim CDInfo() As String
Dim NumDrives As Integer

Public Sub FindCDAtts(CDInfo(), NumDrives)
x = 0
Set ItemSet = GetObject("WinMgmts:").InstancesOf("Win32_CDRomDrive")
For Each Item In ItemSet
x = x + 1
ReDim Preserve CDInfo(1 To x, 2)
CDInfo(x, 1) = Item.Drive
CDInfo(x, 2) = Item.Caption
' info = info & Item.scsiport & ","
' info = info & Item.scsitargetid & ","
' info = info & Item.scsilogicalunit
' CDInfo(x, 2) = info
Next
Set ItemSet = Nothing
Set Item = Nothing
End Sub

Private Sub Command1_Click()
Call FindCDAtts(CDInfo(), NumDrives)
For x = 1 To NumDrives
MsgBox " Drive is " & CDInfo(x, 1) & ", and caption is " & CDInfo(x, 2)
Next x
End Sub


.