Results 1 to 4 of 4

Thread: [RESOLVED] Array and ByRef

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Resolved [RESOLVED] Array and ByRef

    I'm working on code the will be handling large one-dimensional arrays of bytes.

    I've been told in the past that passing an array to a method ByVal can be a big performance hit so I'm enforcing ByRef by wrapping it in a class. The problem with this is that setting Data.ByteArray is still a ByVal operation.

    My question now is this:

    Is there a better way to handle arrays in vb ByRef? I don't want to move my whole app to c# to use pointers as I'm not as familiar with the language. I'm stuck with Byte arrays because IO.MemoryStream requires one and Bitmap.Save requires a stream when working from memory. (I hate temp files with a passion and won't use them.)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Array and ByRef

    Quote Originally Posted by agent
    I've been told in the past that passing an array to a method ByVal can be a big performance hit
    You have been misinformed. Go back to ByVal. There is no reason to pass the array ByRef unless you want to be able to assign a new array object to the parameter inside the method and have the original variable refer to that new array after the method returns.

    The performance is EXACTLY the same whether you pass ByVal or ByRef. Arrays are instances of the Array class, thus they are reference types. That means that an array variable is simply a reference to an array object. References are simply tarted up pointers, so your array variable is just a 32-bit memory address.

    If you pass it by value then the variable is copied. The variable is a 32-bit memory address so your creating a copy of a 32-bit memory address. If you pass it by reference then you're creating a reference to the variable, so you're creating a new 32-bit memory address. Either way, all you're doing is creating a single reference. No array objects are copied at any time.

    The difference, and the ONLY difference, between ByRef and ByVal is that when using ByRef you can assign a new object to the parameter itself and the original variable will then also refer to that new object. That is the same rule regardless of whether the type of the parameter is a value type or a reference type. That is the ONLY thing you need to consider when deciding between ByVal and ByRef. Use ByVal all the time unless you specifically need the behaviour of ByRef.

    Having said all that, I don't think it was that way in VB6, so whoever gave you that information is probably stuck in the past.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Array and ByRef

    Further and simpler, note that value types contain the object in the variable itself. Reference types contain a reference to the object in the variable. When you pass a parameter ByVal you are copying the contents of the variable. That means that passing any reference type object ByVal will copy a reference only. Arrays are reference types so passing an array ByVal copies a reference only.

    When you pass a value type ByVal you are copying the object itself. It's for that reason that it is highly recommended that only small, simple types ever be implemented as structures. Microsoft recommend that any type whose data occupies more than 16 bytes be implemented as a class. 16 bytes is four 32-bit variables, which would include any reference type variables. Remember, a reference is only 32-bits in size, not the size of the object it refers to.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Re: Array and ByRef

    Thanks for the information. I didn't know that an array was a reference type.

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