Property Let/Get and passing arrays 
Author Message
 Property Let/Get and passing arrays

I have a form module with a private array in it.  I've created a property
that exposes the array to the outside world (the rest of the project).

My question is: how can I pass this array to a function that expects an
array for a parameter?

Joel Moore

See code below for example.

In a form called frmTest...

Private mstrArray(1 to 10) As String

Public Property Get ArrayValue(intIndex As Integer) As String
    ArrayValue = mstrArray(intIndex)
End Property

Public Property Let ArrayValue(intIndex As Integer, ByVal strNew As String)
    mstrArray(intIndex) = strNew
End Property

Somewhere in a different code module...

Public Sub Test(strArray() as string)
    'do some stuff with the passed array
End Sub

Public Sub Test2()
    Dim strTest As String

    frmTest.ArrayValue(1) = "One"
    frmTest.ArrayValue(2) = "Two"
    strTest = frmTest.ArrayValue(2)
    ' strTest now = "Two"

    Test(frmTest.ArrayValue) ' <----------This doesn't work

End Sub



Fri, 21 Sep 2001 03:00:00 GMT  
 Property Let/Get and passing arrays

Quote:
> I have a form module with a private array in it.  I've created a property
> that exposes the array to the outside world (the rest of the project).

Actually, what you have is a property that exposes one element of the array
to the outside world.  You are using the supplied index to grab a single
element of your string array instead of returning the entire array.

Quote:
> My question is: how can I pass this array to a function that expects an
> array for a parameter?

You have a couple of options.  The first is to supply a property that
actually provides the entire array.  The property might look something like

Public Property Get WholeArray() As String()
    WholeArray = mstrArray
End Property

In this case, you are no longer specifying an indexing and grabbing that
element from the array but rather passing the entire array.  By specifying
the type as String() you can have fairly strict typing on the property.

If you wanted to be more flexible, you could instead return a variant.  In
this case you might want to use optional parameters to specify passing of an
entire array.  If you go this route, it will mean that you'll have to also
specify variants in the functions you want to pass to.  For instance:

Public Property Get ArrayValue(Optional intIndex As Integer = -1) As Variant
    'If user passed -1 for index, return whole array.  Otherwise, return an
element of array.
    If intIndex < 0 Then
        ArrayValue = mstrArray
    Else
        ArrayValue = mstrArray(intIndex)
    End If
End Property

Now, your Test function must change to accept the different type.  You'll
also want to test to make sure that the proper type was passed (since the
property doesn't enforce the strict typing as an array):

Public Sub Test(strArray As Variant)
    If IsArray(strArray) Then
        'do some stuff with the passed array
    Else
        'they only passed a single element...alert them?
    End If
End Sub

Hope this helps!

Jason



Fri, 21 Sep 2001 03:00:00 GMT  
 Property Let/Get and passing arrays
Joel,

Quote:
>     Test(frmTest.ArrayValue) ' <----------This doesn't work

This is because Test() is expecting an array, and you're trying to pass a
value of the array obtained from the property get (i.e., a string).
Because you can't expose arrays from classes directly, you'll have to find
another way, such as wrapping it in a variant.

For example,

Public Property Get TheWholeArray() As Variant

        Dim vArray As Variant

        vArray = mstrArray()
        TheWholeArray = vArray

End Property

This variant array can then be passed around your procedures with no
problem.

~~~~~~~~~~~~~~~~~~~~~~~~~~~
Please post/reply to the newsgroup(s) so
that everyone can benefit from the discussion.

Regards,

Klaus H. Probst, MCP

       ICQ: 22454937
      The VB Box: http://members.xoom.com/kprobst/
~~~~~~~~~~~~~~~~~~~~~~~~~~~



Quote:
> I have a form module with a private array in it.  I've created a
property
> that exposes the array to the outside world (the rest of the project).

> My question is: how can I pass this array to a function that expects an
> array for a parameter?

> Joel Moore

> See code below for example.

> In a form called frmTest...

> Private mstrArray(1 to 10) As String

> Public Property Get ArrayValue(intIndex As Integer) As String
>     ArrayValue = mstrArray(intIndex)
> End Property

> Public Property Let ArrayValue(intIndex As Integer, ByVal strNew As
String)
>     mstrArray(intIndex) = strNew
> End Property

> Somewhere in a different code module...

> Public Sub Test(strArray() as string)
>     'do some stuff with the passed array
> End Sub

> Public Sub Test2()
>     Dim strTest As String

>     frmTest.ArrayValue(1) = "One"
>     frmTest.ArrayValue(2) = "Two"
>     strTest = frmTest.ArrayValue(2)
>     ' strTest now = "Two"

> End Sub



Fri, 21 Sep 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Property Let/Get and passing arrays

2. Using arrays as arguments in Let and Get Properties

3. property get / property let

4. Property Let, Property Get

5. Property Let vs. Property Set

6. Passing Property Arrays to Sub Routines

7. Passing Arrays to an ActiveX Controls Property

8. Passing Control Array to Class Property

9. let us talk about ARRAYS.

10. how to let a function return an array.

11. Help - LET and GET with Array

12. property let and get,

 

 
Powered by phpBB® Forum Software