Returning an array from a function 
Author Message
 Returning an array from a function

Can anyone give an example of calling a function with two arguments with the
result being an array? or a collection?

Thanks for your help!!!

**********************

I tried it with a collection with no luck...

I have a form and a class.

****

A subroutine in the form calls the function:

Sub GetSystemList()

    Dim objQuerySystemsInDomain As clsQuerySystemsInDomain
    Set objQuerySystemsInDomain = New clsQuerySystemsInDomain

    Dim strSystemList As Collection
    Set strSystemList = New Collection

    Dim strSystemName As Variant

    Set strSystemList =
objQuerySystemsInDomain.QuerySystemsInDomain(strDomainName, strServerType)

    For Each strSystemName In strSystemList
        lstSystemsInDomain.AddItem strSystemName
    Next strSystemName

End Sub

***

Here is the class: (clsQuerySystemsInDomain.cls)

Public Function QuerySystemsInDomain(ByVal strDomainName As String, ByVal
ServerType As String) As Collection

    ...code snipped...

    QuerySystemsInDomain.Add strSystemName

End Function



Sun, 23 Nov 2003 01:11:29 GMT  
 Returning an array from a function
I forgot the:

    Set QuerySystemsInDomain = New Collection

in the class

Opps...


Quote:
> Can anyone give an example of calling a function with two arguments with
the
> result being an array? or a collection?

> Thanks for your help!!!

> **********************

> I tried it with a collection with no luck...

> I have a form and a class.

> ****

> A subroutine in the form calls the function:

> Sub GetSystemList()

>     Dim objQuerySystemsInDomain As clsQuerySystemsInDomain
>     Set objQuerySystemsInDomain = New clsQuerySystemsInDomain

>     Dim strSystemList As Collection
>     Set strSystemList = New Collection

>     Dim strSystemName As Variant

>     Set strSystemList =
> objQuerySystemsInDomain.QuerySystemsInDomain(strDomainName, strServerType)

>     For Each strSystemName In strSystemList
>         lstSystemsInDomain.AddItem strSystemName
>     Next strSystemName

> End Sub

> ***

> Here is the class: (clsQuerySystemsInDomain.cls)

> Public Function QuerySystemsInDomain(ByVal strDomainName As String, ByVal
> ServerType As String) As Collection

>     ...code snipped...

>     QuerySystemsInDomain.Add strSystemName

> End Function



Sun, 23 Nov 2003 01:18:03 GMT  
 Returning an array from a function
Here's a sample of returning a collection..

All it does is return a collection of controls
that are on a form..
'============
Option Explicit

Private Sub Form_Load()
   Dim k As Collection
   Dim c As Control

   Set k = GetControls(Me)
   For Each c In k
      Debug.Print c.Name
   Next
End Sub

Private Function GetControls(frm As Form) As Collection
   Dim tk As Collection
   Dim c As Control

   Set tk = New Collection
   For Each c In frm.Controls
      tk.Add c
   Next
   Set GetControls = tk
End Function
'============


Quote:
> Can anyone give an example of calling a function with two arguments with
the
> result being an array? or a collection?

> Thanks for your help!!!

> **********************

> I tried it with a collection with no luck...

> I have a form and a class.

> ****

> A subroutine in the form calls the function:

> Sub GetSystemList()

>     Dim objQuerySystemsInDomain As clsQuerySystemsInDomain
>     Set objQuerySystemsInDomain = New clsQuerySystemsInDomain

>     Dim strSystemList As Collection
>     Set strSystemList = New Collection

>     Dim strSystemName As Variant

>     Set strSystemList =
> objQuerySystemsInDomain.QuerySystemsInDomain(strDomainName, strServerType)

>     For Each strSystemName In strSystemList
>         lstSystemsInDomain.AddItem strSystemName
>     Next strSystemName

> End Sub

> ***

> Here is the class: (clsQuerySystemsInDomain.cls)

> Public Function QuerySystemsInDomain(ByVal strDomainName As String, ByVal
> ServerType As String) As Collection

>     ...code snipped...

>     QuerySystemsInDomain.Add strSystemName

> End Function



Sun, 23 Nov 2003 02:25:08 GMT  
 Returning an array from a function
Thanks!


Quote:
> Here's a sample of returning a collection..

> All it does is return a collection of controls
> that are on a form..
> '============
> Option Explicit

> Private Sub Form_Load()
>    Dim k As Collection
>    Dim c As Control

>    Set k = GetControls(Me)
>    For Each c In k
>       Debug.Print c.Name
>    Next
> End Sub

> Private Function GetControls(frm As Form) As Collection
>    Dim tk As Collection
>    Dim c As Control

>    Set tk = New Collection
>    For Each c In frm.Controls
>       tk.Add c
>    Next
>    Set GetControls = tk
> End Function
> '============



> > Can anyone give an example of calling a function with two arguments with
> the
> > result being an array? or a collection?

> > Thanks for your help!!!

> > **********************

> > I tried it with a collection with no luck...

> > I have a form and a class.

> > ****

> > A subroutine in the form calls the function:

> > Sub GetSystemList()

> >     Dim objQuerySystemsInDomain As clsQuerySystemsInDomain
> >     Set objQuerySystemsInDomain = New clsQuerySystemsInDomain

> >     Dim strSystemList As Collection
> >     Set strSystemList = New Collection

> >     Dim strSystemName As Variant

> >     Set strSystemList =
> > objQuerySystemsInDomain.QuerySystemsInDomain(strDomainName,
strServerType)

> >     For Each strSystemName In strSystemList
> >         lstSystemsInDomain.AddItem strSystemName
> >     Next strSystemName

> > End Sub

> > ***

> > Here is the class: (clsQuerySystemsInDomain.cls)

> > Public Function QuerySystemsInDomain(ByVal strDomainName As String,
ByVal
> > ServerType As String) As Collection

> >     ...code snipped...

> >     QuerySystemsInDomain.Add strSystemName

> > End Function



Sun, 23 Nov 2003 02:53:39 GMT  
 Returning an array from a function
And here's a sample of returning an array ....

Private Sub Command1_Click()

   Dim a As String
   Dim b As String
   Dim z() As String

   a = "Hello"
   b = "World"

   z = DoSomething(a, b)

   Print z(0)
   Print z(1)

End Sub

Private Function DoSomething(aa As String, bb As String) As Variant

   ReDim xx(0 To 1) As String

   xx(0) = aa
   xx(1) = bb

   DoSomething = xx()

End Function

--

Randy Birch
MVP Visual Basic

http://www.mvps.org/vbnet/

Please respond only to the newsgroups so all can benefit.


: Here's a sample of returning a collection..
:
: All it does is return a collection of controls
: that are on a form..
: '============
: Option Explicit
:
: Private Sub Form_Load()
:    Dim k As Collection
:    Dim c As Control
:
:    Set k = GetControls(Me)
:    For Each c In k
:       Debug.Print c.Name
:    Next
: End Sub
:
: Private Function GetControls(frm As Form) As Collection
:    Dim tk As Collection
:    Dim c As Control
:
:    Set tk = New Collection
:    For Each c In frm.Controls
:       tk.Add c
:    Next
:    Set GetControls = tk
: End Function
: '============
:
:
:


: > Can anyone give an example of calling a function with two arguments with
: the
: > result being an array? or a collection?
: >
: > Thanks for your help!!!
: >
: > **********************
: >
: > I tried it with a collection with no luck...
: >
: > I have a form and a class.
: >
: > ****
: >
: > A subroutine in the form calls the function:
: >
: > Sub GetSystemList()
: >
: >     Dim objQuerySystemsInDomain As clsQuerySystemsInDomain
: >     Set objQuerySystemsInDomain = New clsQuerySystemsInDomain
: >
: >     Dim strSystemList As Collection
: >     Set strSystemList = New Collection
: >
: >     Dim strSystemName As Variant
: >
: >     Set strSystemList =
: > objQuerySystemsInDomain.QuerySystemsInDomain(strDomainName,
strServerType)
: >
: >     For Each strSystemName In strSystemList
: >         lstSystemsInDomain.AddItem strSystemName
: >     Next strSystemName
: >
: > End Sub
: >
: > ***
: >
: > Here is the class: (clsQuerySystemsInDomain.cls)
: >
: > Public Function QuerySystemsInDomain(ByVal strDomainName As String,
ByVal
: > ServerType As String) As Collection
: >
: >     ...code snipped...
: >
: >     QuerySystemsInDomain.Add strSystemName
: >
: > End Function
: >
: >
: >
:
:



Sun, 23 Nov 2003 06:24:22 GMT  
 Returning an array from a function
I'm not sure if it would be considered good "form" or not, but you can eliminate creating
the extra Collection, and eliminate Set(ting) its values to the Function variable at the
end, by using the Function variable directly. (This is equivalent to being able to use the
Function name as a variable directly within the Function's body.)

Rick

Private Function GetControls(frm As Form) As Collection

    Dim c As Control

    Set GetControls = New Collection

    For Each c In frm.Controls
        GetControls.Add c
    Next

End Function



Sun, 23 Nov 2003 19:56:07 GMT  
 Returning an array from a function
I always do it that way!  I think it is perfectly valid code.  Of course,
that my opinion, and who am I anyway?

Tom Shelton


Quote:
> I'm not sure if it would be considered good "form" or not, but you can
eliminate creating
> the extra Collection, and eliminate Set(ting) its values to the Function
variable at the
> end, by using the Function variable directly. (This is equivalent to being
able to use the
> Function name as a variable directly within the Function's body.)

> Rick

> Private Function GetControls(frm As Form) As Collection

>     Dim c As Control

>     Set GetControls = New Collection

>     For Each c In frm.Controls
>         GetControls.Add c
>     Next

> End Function



Mon, 24 Nov 2003 12:51:29 GMT  
 Returning an array from a function

Quote:

> I always do it that way!  I think it is perfectly valid code.  Of course,
> that my opinion, and who am I anyway?

You are the extremely intelligent person who just so happens to this the same way I do
<g>.

Rick



Mon, 24 Nov 2003 14:39:06 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Return entire array from a function

2. returning an array from a function

3. Returning an array from a function

4. Returning an Array from a function

5. returning an array from a function

6. How to return String Arrays from VB functions???

7. returning an array from a function in VB

8. Returning a array from a function

9. Returning an array from a function

10. Returning an array from a function.

11. Array in array returned by function

12. Syntax for function to return an array

 

 
Powered by phpBB® Forum Software