|
-
Mar 19th, 2003, 11:32 AM
#1
Thread Starter
Fanatic Member
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
-
Mar 19th, 2003, 11:34 AM
#2
Frenzied Member
Declare your function as Variant.
-
Mar 19th, 2003, 12:54 PM
#3
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.
-
Mar 19th, 2003, 01:14 PM
#4
Frenzied Member
I'm not quite sure what makes you think that way you do, Shaggy Hiker, so here is a very simple example:
VB Code:
Option Explicit
Private Sub Form_Load()
Dim MyArr(9), i%, SomeData
For i = 0 To 9
MyArr(i) = i
Next
SomeData = ParseArray(MyArr)
For i = LBound(SomeData) To UBound(SomeData)
Debug.Print SomeData(i)
Next i
End Sub
Public Function ParseArray(var As Variant) As Variant
Dim i%, LocalArr()
ReDim LocalArr(0)
For i = LBound(var) To UBound(var)
If i > 0 And var(i) Mod 2 = 0 Then
ReDim Preserve LocalArr(UBound(LocalArr) + 1)
LocalArr(UBound(LocalArr)) = var(i) & " is even number"
End If
Next i
ParseArray = LocalArr
End Function
-
Mar 19th, 2003, 01:29 PM
#5
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.
-
Mar 19th, 2003, 01:35 PM
#6
Frenzied Member
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 ...
-
Mar 19th, 2003, 01:35 PM
#7
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.
-
Mar 19th, 2003, 01:35 PM
#8
Fanatic Member
VB Code:
'declaring the function
Function GetMyArray() As SomeType()
Dim ret() As SomeType
DoStuffToFill
GetMyArray = ret
End Function
'calling the function
Dim result() As SomeType
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.
-
Mar 19th, 2003, 01:38 PM
#9
Radum, I must admit, I didn't even realize you could do that. It seems pretty obvious, now.
That gets my vote.
-
Mar 19th, 2003, 01:39 PM
#10
Frenzied Member
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.
-
Mar 19th, 2003, 01:40 PM
#11
Frenzied Member
Originally posted by radum
VB Code:
'declaring the function
Function GetMyArray() As SomeType()
Dim ret() As SomeType
DoStuffToFill
GetMyArray = ret
End Function
'calling the function
Dim result() As SomeType
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 ...
-
Mar 19th, 2003, 01:45 PM
#12
Hmmm, that quote of mine is almost English. I guess I should have proofed it.
-
Mar 19th, 2003, 01:47 PM
#13
Frenzied Member
-
Mar 19th, 2003, 02:29 PM
#14
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......
-
Mar 19th, 2003, 02:54 PM
#15
Frenzied Member
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.
-
Mar 19th, 2003, 03:05 PM
#16
Frenzied Member
Originally posted by radum
VB Code:
'declaring the function
Function GetMyArray() As SomeType()
Dim ret() As SomeType
DoStuffToFill
GetMyArray = ret
End Function
'calling the function
Dim result() As SomeType
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()
-
Mar 19th, 2003, 03:10 PM
#17
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.
-
Mar 19th, 2003, 03:38 PM
#18
Fanatic Member
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.
-
Mar 19th, 2003, 03:54 PM
#19
Frenzied Member
Originally posted by radum
aammmm.... nope.
My bad...it works both ways. Learn something new every day...
-
Mar 19th, 2003, 04:09 PM
#20
Frenzied Member
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.
-
Mar 19th, 2003, 10:38 PM
#21
PowerPoster
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:
Private Function GetFilesInDirectory(Directory As String) As String()
Dim TheChar As String, File As String, TheArray() As String
TheChar = Mid(Directory, Len(Directory), 1)
If TheChar <> "\" Then Directory = Directory & "\"
File = Dir$(Directory, vbDirectory)
ReDim TheArray(0)
Do While File <> ""
DoEvents
If File <> "." And File <> ".." Then
If (GetAttr(Directory & File) And vbDirectory) <> vbDirectory Then
ReDim Preserve TheArray(UBound(TheArray) + 1)
TheArray(UBound(TheArray)) = File
End If
End If
File = Dir$
Loop
GetFilesInDirectory = TheArray
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:
Private Sub Command1_Click()
For i = 1 To UBound(GetFilesInDirectory(Text1))
List1.AddItem (GetFilesInDirectory(Text1)(i))
Next
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|