Results 1 to 5 of 5

Thread: [RESOLVED] Passing An Array to a Funcion

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Resolved [RESOLVED] Passing An Array to a Funcion

    Yesterday I asked how to have a function pass an arrayas its return value. Now I need to know how to pass the array back to a function. Here is what I currently have, but doesn't this make the variable a variant?
    VB Code:
    1. Public Function MyFunc(SourceArray) As String()
    But if I try this then I get s syntax error:
    VB Code:
    1. Public Function MyFunc(SourceArray As String()) As String()
    Does ParamArray have anything to do with it? I tried using this (without having any idea what I was doing), but to no avail. What does ParamArray do anyway?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Passing An Array to a Funcion

    just guessing....but

    VB Code:
    1. Public Function MyFunc(SourceArray() As String) As String()
    ??

  3. #3

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Passing An Array to a Funcion

    I tried that, and it didn't give me a syntax error, but I never got around to running the program with that format because I thought it would give me an error.

    The first line of code in my original post WORKS, but I think it makes the variable a variant and is therefor obviously slow and I would like to improve it if I can.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Passing An Array to a Funcion

    kfcSmitty - good guess... That is the correct declaration.

    eyeRmonkey - ParamArray allows you to pass multiple arguments to a procedure which VB will then place into an array. The ParamArray argument can only be of type Variant, must be the last one in the procedure's argument list and is always considered optional.

    VB Code:
    1. Public Function CountNumbers(ParamArray Numbers() As Variant) As Long
    2.     Dim lngIdx As Long
    3.    
    4.     For lngIdx = LBound(Numbers) To UBound(Numbers)
    5.         CountNumbers = CountNumbers + Numbers(lngIdx)
    6.     Next
    7.    
    8. End Function
    9.  
    10. Private Sub Form_Load()
    11.  
    12. Debug.Print CountNumbers(1, 4.5, 7, 100)
    13. Debug.Print CountNumbers(84, 2)
    14. Debug.Print CountNumbers
    15.  
    16. End Sub

  5. #5

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Passing An Array to a Funcion

    Ok, will use Smitty's way. Thanks guys.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

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