Click to See Complete Forum and Search --> : function and argument
PsSheba
June 21st, 2001, 06:49 AM
My problem:
I create an array of strings of an unknown size.
The size of the array varies according to user's acrion.
I want to send the array to a function which accepts as an argument a string.
( Public function MyFunction(strMyArrayOfStrins))
In the function i want to do some actions on that array but the function treats it as a string parameter, not an array.
How do i send, or how do i declare the function's argument so that the function will know that this is an array ?
Thank in advance !
Cimperiali
June 21st, 2001, 07:01 AM
option Explicit
private Sub Command1_Click()
Dim myarray() as string
Dim ret as string
ReDim myarray(10)
ret = fnctOfArray(myarray)
MsgBox ret
End Sub
private Function fnctOfArray(received() as string) as string
fnctOfArray = "There are " & UBound(received) + 1 & " elemet in the array"
End Function
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
Iouri
June 21st, 2001, 07:07 AM
Here is an example of the function that returns an array
Function that returns an array
New in VB6, functions can return arrays. For example:
' Return an array of strings.
Private Function NumberList() As String()
Dim arr() As String
Dim i As Integer
ReDim arr(1 To 10)
For i = 1 To 10
arr(i) = Format$(i) & " * " & Format$(i) & " = " & Format$(i * i)
Next i
NumberList = arr
End Function
The program could invoke this function as in:
Dim strings() As String
strings = NumberList()
This assignment may not work if the array is declared statically (using
explicit bounds) and the bounds do not match those of the array returned
by the function.
Iouri Boutchkine
iouri@hotsheet.com
Iouri
June 21st, 2001, 07:09 AM
Here is an example how to pass array as parameter
Dim aryTest(1 To 3)
Private Sub Command1_Click()
TestArray aryTest
TestArray
End Sub
Private Sub Form_Load()
aryTest(1) = 1
aryTest(2) = 2
aryTest(3) = 3
End Sub
Private Sub TestArray(Optional aryToTest)
If IsMissing(aryToTest) Then
MsgBox "Array Not Passed"
Else
For intCount = 1 To 3
strMessage = strMessage & "Element " & CStr(intCount) & " = " & CStr(aryToTest(intCount)) & vbLf
Next
MsgBox strMessage
End If
End Sub
Iouri Boutchkine
iouri@hotsheet.com
Cimperiali
June 21st, 2001, 07:09 AM
'send an array of string to a sub
'it is byref, so your changes will reflect on the original array:
option Explicit
private Sub Command1_Click()
Dim myarray() as string
ReDim myarray(10)
call subOfArray(myarray)
MsgBox myarray(0)
End Sub
private sub SubOfArray(received() as string)
received(0) = "There are " & UBound(received) + 1 & " elemet in the array"
End Sub
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
PsSheba
June 21st, 2001, 08:06 AM
Some more answers like this and i'll be a guru myself !
Thanx !
PsSheba
June 21st, 2001, 08:08 AM
Thank you !
Iouri
June 21st, 2001, 08:10 AM
Rate it if it helped you
Iouri Boutchkine
iouri@hotsheet.com
PsSheba
June 21st, 2001, 08:10 AM
Thanx again
PsSheba
June 21st, 2001, 08:14 AM
I try to rate all the answers i got but i get an error message: No ersponse from server "!
I'll do it later. Your answer helped me and i'm gratefull to you !
Cimperiali
June 21st, 2001, 08:25 AM
...Mmhm... do this: try again with Iouri. As soon as you see the server error, click on "back" button of your browser. Then refresh the page. I think you'll see the page again, but with your vote setted!
;)
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
Cimperiali
June 21st, 2001, 08:27 AM
..and Now welcome to The_Raters Club (of which - of course -I am the President and which - of course - does not exist)
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
deghost
June 21st, 2001, 02:48 PM
You can put a certain sign in the end of each string in the array before you make the "strings array-string". Chr(10) for example and that way you can always know when one string ends and another begins.
x2=InStr(x+1, str, Chr(10), vbBinaryCompare)-x
s1=mid$(str, x+1, x2 - 1)
x=x2+x
----------
The @host is everywhere!
----------
PsSheba
June 22nd, 2001, 05:50 AM
Thats why i recieved an error message.
i rated all 3 answers giving each one different rate. (the first one i read i gave 10, the second 6 and the third 3) is it ok ?
your (yours and iouri's) answers are important to me and i hope the rating is ok ! if not, please let me know so in the future i do better !
PsSheba
June 22nd, 2001, 05:53 AM
Sorry ! i didnt understand !
i need more simple answer !
Thank you any way !
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.