473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How do I use use multiple arrays with one function?

T
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
Aug 18 '05 #1
3 6794

"T" <gi****@hotpop.com> wrote in message
news:e4********************************@4ax.com...
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?

Sure - use an array!

You could use a two dimensional array:
Dim MyArray(1 to 7, 1 to 100) As MyData

in which case the code looks like
array(n, x).data23 = 43

Or you could declare another type (I like this better):
Type MyDataArray
Arr(1 to 100) As MyData
End Type

Dim MyArrays(1 to 7) As MyDataArray

and the code would look like
MyArrays(n).Arr(x) = 43
Aug 19 '05 #2
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
Jul 29 '06 #3
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

Aug 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: rl | last post by:
Hi out there, I'd like to know sth about the costs of a function call in php and the handling of character arrays (init size, enlargement steps of allocated memory, technique on enlargement ->...
2
by: Jason | last post by:
I have a number of arrays that are populated with database values. I need to determine which array has the highest ubound out of all the arrays. The array size will always change based on the...
17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
4
by: Andrew | last post by:
Hello, I am recieving a multiple definition error from the linker when I try to build a project I am working on. The message states that the functions I defined within an external .c source file...
0
by: Tom | last post by:
Shouldn't you be able to return them as 3 By Ref arguments in the function call? >-----Original Message----- >I have a class with a function which need to return to the caller three...
5
by: vj | last post by:
Hi all, I am using C++Builder-5. I want to run multiple cpp files at the same time. If I use the C++Builder for running a cpp file (i.e., I just double click the cpp file, it then opens in the...
4
by: Nathan Sokalski | last post by:
I am a beginner with AJAX, and have managed to learn how to use it when passing single parameters, but I want to return more than one value to the client-side JavaScript function that displays it....
7
by: psybert | last post by:
Hello everyone, Long time lurker, first time poster. I'm a beginner coder, and I've taught myself everything with the help and expertise of users and websites like this one. I normally figure out...
2
by: beargrease | last post by:
I'm kind of comfortable with basic joins, but a current project requires a complex query of many tables. The GROUP_CONCAT(DISTINCT ...) function has been very useful as returning my values as comma...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.