CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    May 2001
    Location
    israel
    Posts
    112

    function and argument

    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 !


  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: function and argument


    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.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: function and argument

    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
    [email protected]
    Iouri Boutchkine
    [email protected]

  4. #4
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: function and argument

    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
    [email protected]
    Iouri Boutchkine
    [email protected]

  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: And if you want send and receive back...


    '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.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #6
    Join Date
    May 2001
    Location
    israel
    Posts
    112

    and thank you too !

    Some more answers like this and i'll be a guru myself !
    Thanx !


  7. #7
    Join Date
    May 2001
    Location
    israel
    Posts
    112

    Re: function and argument

    Thank you !


  8. #8
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: function and argument

    Rate it if it helped you

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  9. #9
    Join Date
    May 2001
    Location
    israel
    Posts
    112

    Re: function and argument

    Thanx again


  10. #10
    Join Date
    May 2001
    Location
    israel
    Posts
    112

    No response from server !

    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 !


  11. #11
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: No response from server !

    ...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.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  12. #12
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Ok, you managed it!

    ..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.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  13. #13
    Join Date
    Jun 2001
    Location
    Israel
    Posts
    228

    Re: function and argument

    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!
    ----------

  14. #14
    Join Date
    May 2001
    Location
    israel
    Posts
    112

    Re: i didnt rate before pressing the button !

    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 !


  15. #15
    Join Date
    May 2001
    Location
    israel
    Posts
    112

    Re: function and argument

    Sorry ! i didnt understand !
    i need more simple answer !
    Thank you any way !


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

Featured