Results 1 to 5 of 5

Thread: How to send arrays to a function

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    65

    How to send arrays to a function

    How can i send arrays to a function?

    example i have an array with 400 elements containing random numbers how can i send those to a function, say to add 1 to each them?

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to send arrays to a function

    You define your sub or function to take an array of the given type as a parameter then you pass your array as a parameter.

    Code:
    Private Function DoSomething(ByVal MyArray as Integer()) as Integer()
    'dosomething with the array
    return MyArray
    end function
    You could optionally use a sub and ByRef to work directly with your existing array depending on your needs.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to send arrays to a function

    you can use a lambda function for that:

    vb Code:
    1. Dim numbers() As Integer = Enumerable.Range(1, 400).ToArray
    2. numbers = Array.ConvertAll(numbers, Function(x) x + 1)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to send arrays to a function

    Quote Originally Posted by DataMiser View Post
    You could optionally use a sub and ByRef to work directly with your existing array depending on your needs.
    There is no need to use ByRef. All arrays are instances of the Array class. Just like all class instances, they are reference type objects. As such, there is no copy made and any changes you make to the one and only Array object are reflected in the caller. Just like all classes, the only reason you would need to use ByRef was if you actually wanted to assign a new object to the parameter inside the method.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to send arrays to a function

    Quote Originally Posted by jmcilhinney View Post
    There is no need to use ByRef. All arrays are instances of the Array class. Just like all class instances, they are reference type objects. As such, there is no copy made and any changes you make to the one and only Array object are reflected in the caller. Just like all classes, the only reason you would need to use ByRef was if you actually wanted to assign a new object to the parameter inside the method.
    I did not know that, Thanks

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