To the OP, as I can only see John's response to this question ...
In VB6, functions can return user-defined types if so declared. As you have
seven sets of numbers you have to assign to the UDTs at various times you
will still require a mechanism to utilize the proper set of values within
the function, in order that your array(x) has the proper values based on the
option.
Try this out, requiring only a command button to test. It utilized the
Array() function of VB to store and deliver to a() the set of numbers
corresponding to the option required. (For brevity I only coded 5 numbers,
rather than all 23.) Upon calling the function the appropriate set of
numbers is assigned to a(), which in turn is assigned as the return values
for the function. These results are assigned directly to the UDT you
specify. Note that to enable a loop to iterate through the values to assign
them to the function result, I have altered your UDT to eliminate the
repetitive .data1, .data2 ... data23 variables and replaced them all with
one variable -- data() -- which itself is an array. So your UDT is
comprised of two arrays -- one, the array corresponding to options 1 through
7 (eliminating the array1(), array2() array3 etc), and a second array
corresponding to the 23 elements (defined 0 to 22 as the Array function is
0-based).
Private Type MyDataTypeArray
data(0 To 22) As Long '23 elements
End Type
Private Sub Command1_Click()
ReDim result(1 To 7) As MyDataTypeArray
Dim optNo As Long
Dim cnt As Long
'---------------------
optNo = 1
result(optNo) = FillMyTypeArray(optNo)
For cnt = LBound(result(optNo).data) To UBound(result(optNo).data)
Debug.Print result(optNo).data(cnt),
Next
Debug.Print
'---------------------
optNo = 2
result(optNo) = FillMyTypeArray(optNo)
For cnt = LBound(result(optNo).data) To UBound(result(optNo).data)
Debug.Print result(optNo).data(cnt),
Next
Debug.Print
'---------------------
optNo = 3
result(optNo) = FillMyTypeArray(optNo)
For cnt = LBound(result(optNo).data) To UBound(result(optNo).data)
Debug.Print result(optNo).data(cnt),
Next
End Sub
Private Function FillMyTypeArray(optNo As Long) As MyDataTypeArray
Dim a() As Variant
Dim cnt As Long
Select Case optNo
Case 1: a() = Array(36, 23, 34, 46, 69) 'add more to get 23 members
Case 2: a() = Array(43, 77, 99, 22, 44)
Case 3: a() = Array(78, 63, 99, 25, 22)
Case 4: 'etc for other option numbers
Case 5
Case 6
Case 7
End Select
For cnt = LBound(a) To UBound(a)
FillMyTypeArray.data(cnt) = a(cnt)
Next
End Function
--
Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/
Please reply to the newsgroups so all can participate.
"John F. Eldredge" <jo**@jfeldredge.comwrote in message
news:mo********************************@4ax.com...
On Thu, 18 Aug 2005 16:08:54 GMT, T <gi****@hotpop.comwrote:
>Hi all. I have a problem I have not been able to find a reference
about. I am using VB6 and am only a hobbyist programmer. I have 7
arrays of type MyData. Type MyData has 23 elements. Which array that
is operated on depends on 1 of 7 options Currently if I want to write
to an array I have to do this:
if opt1 then
array1(x).data1= 36
array1(x).data2= 23
..
array1(x).data23= 69
else
if opt2 then
array2(x).data1 = 43
array2(x).data2 =77
..
array2(x).data23 =44
else
if opt3 then
array3(x).data1 = 78
array3(x).data2 = 63
..
array3(x).data23 =22
end if
end if
end if
I would like to use a shorter routine to write/read these arrays
rather than go through 7 long if statements. Perhaps something like
below that would pick the array to write to:
sub write (array to use)
array(x).data1= 36
array(x).data2= 23
..
array(x).data23= 69
end sub
Is there a way to do this?
Thanks in advance
John
You could pass the array as an argument to the function. For example,
>Option Explicit
Option Compare Text
Private arr1 As Variant
Private arr2 As Variant
Private Sub Command1_Click()
listarray arr1
End Sub
Private Sub Command2_Click()
listarray arr2
End Sub
Private Sub Form_Load()
arr1 = Array("alpha", "bravo", "charlie")
arr2 = Array("delta", "echo", "foxtrot")
End Sub
Private Sub listarray(ByRef ThisArray As Variant)
Dim intIndex
If IsArray(ThisArray) Then
For intIndex = LBound(ThisArray) To UBound(ThisArray)
MsgBox ThisArray(intIndex)
Next
Else
MsgBox "argument passed isn't an array"
End If
End Sub
where command1 and command2 are the names of command buttons.
--
John F. Eldredge --
jo**@jfeldredge.com
PGP key available from
http://pgp.mit.edu
"Reserve your right to think, for even to think wrongly is better
than not to think at all." -- Hypatia of Alexandria