Results 1 to 21 of 21

Thread: How do I return a array in a functions

  1. #1

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720

    How do I return a array in a functions

    In a function, Lets say a functi0n that lists all the dirs, how can I return an array?

    dim LawL() as string
    LawL = ListDir("C:\WINDOWS")
    then each value in the LawL() array is a filename in C:\WINDOWS. Kind of like how the split() function returns an array
    asdf

  2. #2
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Declare your function as Variant.
    McGenius

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    I can't think of a solution (including McGenius's) that doesn't make me wince.

    The solution McGenius suggested has the problem of using variants, which are kind of painful to some people.

    Another possibility which is also painful, but may be faster, is to pass the array to the function, relying on pass by reference (the default for VB6). The function fills the array, and it is accessible from the calling function. Of course, this relies on a side effect, and won't make some people very happy. It should be fast, however.

    An OO solution would be to wrap the array and means for using the data in the array into an object. The array would be a member of the object, while the function would be a private member function. This isn't cleanly done in VB6, though it is in VB.NET, however, the structure may suggest some solution.

  4. #4
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    I'm not quite sure what makes you think that way you do, Shaggy Hiker, so here is a very simple example:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. Dim MyArr(9), i%, SomeData
    5.  
    6.     For i = 0 To 9
    7.         MyArr(i) = i
    8.     Next
    9.     SomeData = ParseArray(MyArr)
    10.     For i = LBound(SomeData) To UBound(SomeData)
    11.         Debug.Print SomeData(i)
    12.     Next i
    13.  
    14. End Sub
    15.  
    16. Public Function ParseArray(var As Variant) As Variant
    17. Dim i%, LocalArr()
    18.  
    19.     ReDim LocalArr(0)
    20.     For i = LBound(var) To UBound(var)
    21.         If i > 0 And var(i) Mod 2 = 0 Then
    22.             ReDim Preserve LocalArr(UBound(LocalArr) + 1)
    23.             LocalArr(UBound(LocalArr)) = var(i) & " is even number"
    24.         End If
    25.     Next i
    26.     ParseArray = LocalArr
    27.  
    28. End Function
    McGenius

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    I don't think he was saying that it is a bad way... just that there really isn't a "good, clean" way to do itin VB6...... I agree w/ his assesment of using variants..... while it works, it wouldn't be my preference.... I'd opt for passing it in as a parameter, rather than returning as a function.....
    Passing static arrays isn't usually a problem since they are of a defined size.... it's passing dynamic arrays where the trouble begins..... it doesn't know how big it's going to be, which is why it has to be passed around as a variant..... but then you run the risk of losing the ability to strong cast the data and could potentially get bad data in there..... it's a risk... as long as you are aware of it and take the necessary steps (error handling - no ON ERROR RESUME NEXT _does not_ count) all should be OK.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Originally posted by techgnome
    ... Passing static arrays isn't usually a problem since they are of a defined size.... it's passing dynamic arrays where the trouble begins ...
    What in world are you talking about ??? Look at that simple function I wrote one more time and tell me where did you see static array ??? And what so bad and painfull about it ??? I'm not a bif fan of arrays but what you and the other guy are saying is just pure nonsense. Common, really ...
    McGenius

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    I understood what you were getting at McGenius, and your example looks good. My squeamishness about the variant data type comes from C++. The variant data type is like the Run Time Type ID that became available in standard C++. Identifying the type incurrs a small overhead. Currently, that's insignificant in VB, but I seem to remember reading back in an early incarnation that for performance reasons we should avoid the type. From what I have read, RTTI is regarded much like "goto" in that some people think it is useful while others we state that there is NEVER any need for it. I feel the same way about variants, though current technology probably makes my position unsuportable.

  8. #8
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    VB Code:
    1. 'declaring the function
    2. Function GetMyArray() As SomeType()
    3. Dim ret() As SomeType
    4.  
    5. DoStuffToFill
    6.  
    7. GetMyArray = ret
    8.  
    9. End Function
    10.  
    11. 'calling the function
    12. Dim result() As SomeType
    13. result() = GetMyArray()
    variants also work, theres not too much difference, but this one kinda makes it look more obvious that an array is returned.
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    Radum, I must admit, I didn't even realize you could do that. It seems pretty obvious, now.

    That gets my vote.

  10. #10
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Originally posted by Shaggy Hiker
    ... From what I have read, RTTI is regarded much like "goto" in that some people think it is useful while others we state that there is NEVER any need for it ...
    It depends on HOW COMFORTABLE you are with anything but arrays (collections, perhaps) I guess.

    Cheers.
    McGenius

  11. #11
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Originally posted by radum
    VB Code:
    1. 'declaring the function
    2. Function GetMyArray() As SomeType()
    3. Dim ret() As SomeType
    4.  
    5. DoStuffToFill
    6.  
    7. GetMyArray = ret
    8.  
    9. End Function
    10.  
    11. 'calling the function
    12. Dim result() As SomeType
    13. result() = GetMyArray()
    variants also work, theres not too much difference, but this one kinda makes it look more obvious that an array is returned.
    Absolutely, bravo ...
    McGenius

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    Hmmm, that quote of mine is almost English. I guess I should have proofed it.

  13. #13
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    ;-)
    McGenius

  14. #14
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by McGenius
    What in world are you talking about ??? Look at that simple function I wrote one more time and tell me where did you see static array ??? And what so bad and painfull about it ??? I'm not a bif fan of arrays but what you and the other guy are saying is just pure nonsense. Common, really ...
    What I wrote wasn't direct at you (ok, maybe the first two sentences were, but the rest wasn't)..... Nor did I say there there was anything wrong with the code snippet..... I was just voicing an opinion. If I had my way the variant "type" wouldn't exist..... toss it out.... it's not necessary.....
    It depends on HOW COMFORTABLE you are with
    -- that's how it is with everything in programming......
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Originally posted by techgnome
    What I wrote wasn't direct at you (ok, maybe the first two sentences were, but the rest wasn't)..... Nor did I say there there was anything wrong with the code snippet...
    I wasn't asking about that snippet I've posted - I know it works (wouldn't post it otherwise).

    Originally posted by techgnome
    ... If I had my way the variant "type" wouldn't exist..... toss it out.... it's not necessary...
    Hmm, interesting concept ?! May I ask you how would get anything from say winsock without knowing what type to expect? Many of web project would just die before they even deployed... I may continue this list for a long time - just hoping that you've already got the point by now.

    Cheers.
    McGenius

  16. #16
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Originally posted by radum
    VB Code:
    1. 'declaring the function
    2. Function GetMyArray() As SomeType()
    3. Dim ret() As SomeType
    4.  
    5. DoStuffToFill
    6.  
    7. GetMyArray = ret
    8.  
    9. End Function
    10.  
    11. 'calling the function
    12. Dim result() As SomeType
    13. result() = GetMyArray()
    variants also work, theres not too much difference, but this one kinda makes it look more obvious that an array is returned.
    The only correction I would make is that when you do the assignment, leave off the parenthesis from the receiver.

    result = GetMyArray()
    ~seaweed

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109
    I do use a variant to get data from winsock, but I would have preferred that Microsoft handle that differently. I think they could have handled that with a string.

  18. #18
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    Originally posted by seaweed
    The only correction I would make is that when you do the assignment, leave off the parenthesis from the receiver.

    result = GetMyArray()
    aammmm.... nope.
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  19. #19
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Originally posted by radum
    aammmm.... nope.
    My bad...it works both ways. Learn something new every day...
    ~seaweed

  20. #20
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Originally posted by Shaggy Hiker
    I do use a variant to get data from winsock, but I would have preferred that Microsoft handle that differently. I think they could have handled that with a string.
    It's not just MS but everybody else plus MS and it's not about our preferences - quite often technology strikes right back at you when you are pushing to the limit.
    McGenius

  21. #21
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    I don't know what all this talk about using variants and stuff is for becasue this can be easily accomplished with a string array.

    Example: This function takes a directory as input then spits back out all the files in that directory in a string array.

    VB Code:
    1. Private Function GetFilesInDirectory(Directory As String) As String()
    2. Dim TheChar As String, File As String, TheArray() As String
    3. TheChar = Mid(Directory, Len(Directory), 1)
    4. If TheChar <> "\" Then Directory = Directory & "\"
    5. File = Dir$(Directory, vbDirectory)
    6. ReDim TheArray(0)
    7. Do While File <> ""
    8. DoEvents
    9.  If File <> "." And File <> ".." Then
    10.      If (GetAttr(Directory & File) And vbDirectory) <> vbDirectory Then
    11.       ReDim Preserve TheArray(UBound(TheArray) + 1)
    12.      TheArray(UBound(TheArray)) = File
    13.      End If
    14.  End If
    15. File = Dir$
    16. Loop
    17. GetFilesInDirectory = TheArray
    18. End Function


    TO use it i made an example form that has a textbox a list box and a button.

    Type the directory you want to list in the text box then click the button and the listbox will populate.

    Heres the code in the button.

    VB Code:
    1. Private Sub Command1_Click()
    2. For i = 1 To UBound(GetFilesInDirectory(Text1))
    3. List1.AddItem (GetFilesInDirectory(Text1)(i))
    4. Next
    5. End Sub

    You could even add a filter to the function so as to return only certain types of files pretty easily.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width