How do I use use multiple arrays with one function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • T

    How do I use use multiple arrays with one function?

    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).data2 3= 69
    else
    if opt2 then
    array2(x).data1 = 43
    array2(x).data2 =77
    ..
    array2(x).data2 3 =44
    else
    if opt3 then
    array3(x).data1 = 78
    array3(x).data2 = 63
    ..
    array3(x).data2 3 =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
  • Steve Gerrard

    #2
    Re: How do I use use multiple arrays with one function?


    "T" <giveup@hotpop. com> wrote in message
    news:e4a9g158g9 nugi2emibb7ovp6 6i0r73g30@4ax.c om...[color=blue]
    > 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).data2 3= 69
    > else
    > if opt2 then
    > array2(x).data1 = 43
    > array2(x).data2 =77
    > ..
    > array2(x).data2 3 =44
    > else
    > if opt3 then
    > array3(x).data1 = 78
    > array3(x).data2 = 63
    > ..
    > array3(x).data2 3 =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?
    >[/color]


    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


    Comment

    • John F. Eldredge

      #3
      Re: How do I use use multiple arrays with one function?

      On Thu, 18 Aug 2005 16:08:54 GMT, T <giveup@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).data2 3= 69
      >else
      if opt2 then
      array2(x).data1 = 43
      array2(x).data2 =77
      ..
      array2(x).data2 3 =44
      else
      if opt3 then
      array3(x).data1 = 78
      array3(x).data2 = 63
      ..
      array3(x).data2 3 =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(ThisArr ay) Then
      For intIndex = LBound(ThisArra y) To UBound(ThisArra y)
      MsgBox ThisArray(intIn dex)
      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 -- john@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

      Comment

      • Randy Birch

        #4
        Re: How do I use use multiple arrays with one function?

        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(o ptNo).data) To UBound(result(o ptNo).data)
        Debug.Print result(optNo).d ata(cnt),
        Next
        Debug.Print

        '---------------------
        optNo = 2
        result(optNo) = FillMyTypeArray (optNo)

        For cnt = LBound(result(o ptNo).data) To UBound(result(o ptNo).data)
        Debug.Print result(optNo).d ata(cnt),
        Next
        Debug.Print


        '---------------------
        optNo = 3
        result(optNo) = FillMyTypeArray (optNo)

        For cnt = LBound(result(o ptNo).data) To UBound(result(o ptNo).data)
        Debug.Print result(optNo).d ata(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


        Please reply to the newsgroups so all can participate.




        "John F. Eldredge" <john@jfeldredg e.comwrote in message
        news:moonc25siu mpn5gq45dd2ic1r qun4uast4@4ax.c om...
        On Thu, 18 Aug 2005 16:08:54 GMT, T <giveup@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).data2 3= 69
        >else
        if opt2 then
        array2(x).data1 = 43
        array2(x).data2 =77
        ..
        array2(x).data2 3 =44
        else
        if opt3 then
        array3(x).data1 = 78
        array3(x).data2 = 63
        ..
        array3(x).data2 3 =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(ThisArr ay) Then
        For intIndex = LBound(ThisArra y) To UBound(ThisArra y)
        MsgBox ThisArray(intIn dex)
        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 -- john@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

        Comment

        Working...