Passing arguments into functions that expect an array 
Author Message
 Passing arguments into functions that expect an array

(This is related to a previous posting, but I realized
that the problem was more general, so I'm moving the
discussion here)

I'm trying to pass things into functions that expect
arrays.  

I.  For the sake of discussion, a few function definitions:

function myfunction(arr() as Integer)
   ' Doesn't matter what the contents of this function are
end function

function GetArray()
   Dim arr(2) as Integer
   arr(0) = 0
   arr(1) = 1
   arr(2) = 2
   GetArray = arr
end function

II.  I'm trying to figure out the right way to do the
following:

1.  Pass a parameter into myfunction(...) indicating that
there are no elements
2.  Pass the return value from GetArray into the function

III.  What I've been doing...

I've been declaring functions of that sort to take
a "VARIANT" argument instead of what I described above,
namely:

function myfunction(arrIntegers as Variant)
    ' ...
end function

and followed the convention that functions of this sort
would only be passed either Null or a valid array.

IV.  Why that isn't a complete solution

I want to take advantage of VB6's IDE Auto-completion
functions, and if I declare an argument as follows:

function myfunction(arrCustomObjects() as CCustomObject)
    ' ...
end function

... I can take advantage of the auto-complete function
everytime I reference an element of the array.

V.  Sourcecode

Private sub Command1_Click
   Dim arrIntegers() as Integer

   ' this line generates a compile error
   arrIntegers = null

   ' this line generates a compile error
   set arrIntegers = nothing

   ' This line also generates a compile error
   myfunction null

   ' This line also generates a compile error
   myfunction nothing

   ' This line does not generate a compile error,
   ' but the array contains one element.  I want it to
   ' contain zero elements.
   redim arrIntegers(0)
   myfunction arrIntegers

   ' This line generates a compile error and
   ' relates to the second case
   myfunction GetArray()
end sub

Thank you in advance,
Brian Rosenthal



Tue, 07 Dec 2004 02:09:35 GMT  
 Passing arguments into functions that expect an array

Quote:
> (This is related to a previous posting, but I realized
> that the problem was more general, so I'm moving the
> discussion here)

Would passing an uninitialized variable array work for you?

Private Sub Main()
Dim i() As Integer
Dim j(1) As Integer
myfunction i
myfunction j
End Sub

Function myfunction(arr() As Integer)
Dim x As Long
On Error GoTo noarray
x = LBound(arr)
MsgBox "array was passed", vbOKOnly, "Yes"
Exit Function

noarray:
MsgBox "no data passed", vbOKOnly, "No"
End Function



Tue, 07 Dec 2004 03:59:31 GMT  
 Passing arguments into functions that expect an array

Okay, I've resolved my issue.  I'm going to stop declaring
arrays as parameters.  Too messy, I think.  Instead, I'm
going to use a Collection object, which seems more elegant
to manipulate anyway.

Thank you, Bob Butler and Doug Marquardt...

--Brian

Quote:
>-----Original Message-----

message

>> (This is related to a previous posting, but I realized
>> that the problem was more general, so I'm moving the
>> discussion here)

>Would passing an uninitialized variable array work for
you?

>Private Sub Main()
>Dim i() As Integer
>Dim j(1) As Integer
>myfunction i
>myfunction j
>End Sub

>Function myfunction(arr() As Integer)
>Dim x As Long
>On Error GoTo noarray
>x = LBound(arr)
>MsgBox "array was passed", vbOKOnly, "Yes"
>Exit Function

>noarray:
>MsgBox "no data passed", vbOKOnly, "No"
>End Function

>.



Tue, 07 Dec 2004 12:01:23 GMT  
 Passing arguments into functions that expect an array

Quote:

> (This is related to a previous posting, but I realized
> that the problem was more general, so I'm moving the
> discussion here)

> I'm trying to pass things into functions that expect
> arrays.

> I.  For the sake of discussion, a few function definitions:

> function myfunction(arr() as Integer)
>    ' Doesn't matter what the contents of this function are
> end function

> function GetArray()
>    Dim arr(2) as Integer
>    arr(0) = 0
>    arr(1) = 1
>    arr(2) = 2
>    GetArray = arr
> end function

> II.  I'm trying to figure out the right way to do the
> following:

> 1.  Pass a parameter into myfunction(...) indicating that
> there are no elements
> 2.  Pass the return value from GetArray into the function

> III.  What I've been doing...

> I've been declaring functions of that sort to take
> a "VARIANT" argument instead of what I described above,
> namely:

> function myfunction(arrIntegers as Variant)
>     ' ...
> end function

> and followed the convention that functions of this sort
> would only be passed either Null or a valid array.

> IV.  Why that isn't a complete solution

> I want to take advantage of VB6's IDE Auto-completion
> functions, and if I declare an argument as follows:

> function myfunction(arrCustomObjects() as CCustomObject)
>     ' ...
> end function

> ... I can take advantage of the auto-complete function
> everytime I reference an element of the array.

Resolution to this...
Public Function myFunction(arr As Variant)
    If Not IsArray(arr) Or IsEmpty(arr) Or IsNull(arr) Then
        Err.Raise 5
    End If
    If IsEmpty(arr) Or IsNull(arr) Then Exit Function

    Dim elem as CCustomObject

    '...

    Set elem = arr(1) 'And so on...
End Function

Quote:
> V.  Sourcecode

> Private sub Command1_Click
>    Dim arrIntegers() as Integer

>    ' this line generates a compile error
>    arrIntegers = null

Yup.  No real way to do this.  I don't think it's even possible to
do the equivalent in C/C++, since I think you have to dimension an
array to specify a particular type of contents.

Quote:
>    ' this line generates a compile error
>    set arrIntegers = nothing

Yup.  An array is a SAFEARRAY, not an IDispatch or IUnknown.

Quote:
>    ' This line also generates a compile error
>    myfunction null

If the argument isn't a variant, then Null won't match, I don't believe.

Quote:
>    ' This line also generates a compile error
>    myfunction nothing

If the argument isn't a variant or an object type, then Nothing won't
match.

Quote:
>    ' This line does not generate a compile error,
>    ' but the array contains one element.  I want it to
>    ' contain zero elements.
>    redim arrIntegers(0)
>    myfunction arrIntegers

>    ' This line generates a compile error and
>    ' relates to the second case
>    myfunction GetArray()
> end sub

GetArray doesn't return an array.  It returns a Variant containing an
array.  If you're using VB6, you need to change the declaration to,

Function GetArray() As Integer()

If you're using VB5, you can't return an array except inside a Variant.



Mon, 13 Dec 2004 03:21:12 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. pass an array as function/sub argument

2. pass an array as function/sub argument

3. passing arguments to sub - script not working as expected

4. Passing Arguments / Passing an Array

5. 2 questions: array and argument passing.

6. 2 questions: array and argument passing.

7. HELP!! passing an array as an argument

8. Passing Control Arrays as Arguments

9. Passing dynamic array as argument to DLL.

10. How to modify array passed as arguments

11. Passing array as arguments to a DLL subroutine

12. How to pass arrays as arguments?

 

 
Powered by phpBB® Forum Software