Array ByVal or ByRef in VB.NET?-VBForums
Results 1 to 10 of 10

Thread: Array ByVal or ByRef in VB.NET?

  1. #1

    Thread Starter
    Fanatic Member sbasak's Avatar
    Join Date
    Aug 2001
    Location
    Globe Trotter
    Posts
    524

    Array ByVal or ByRef in VB.NET?

    In VB6, arrays are always passed by Reference. In VB.NET, the default parameter passed by value. But is the ByVal holds true for arrays as well by default??

    Thanx
    Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
    Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.

  2. #2
    Elite Member
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,081
    Yes , all data types in Visual Basic.NET are passed ByVal by default .

  3. #3
    Member
    Join Date
    Sep 2002
    Location
    California
    Posts
    52
    There are two types in .NET, value types and reference types.

    Value types are the basic types, such as int, long, double, etc. This includes structures and enums. As you would guess, value types are passed by value by default.

    Reference types are objects, which is pretty much everything else not mentioned above. This includes arrays and strings. As you would guess, reference types are passed by reference by default.

  4. #4
    Elite Member
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,081
    That's right wyrd , referenced type passes a pointer to a memory allocation .

  5. #5
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261
    However, the VS.NET IDE automatically puts 'ByVal' in front of every parameter when you write a function, unless you specify otherwise.

  6. #6

    Thread Starter
    Fanatic Member sbasak's Avatar
    Join Date
    Aug 2001
    Location
    Globe Trotter
    Posts
    524
    In VB6, array was always passed by reference. That mean in VB.NET, we can pass an array both by value & by reference. isn't it??

    Thanx
    Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
    Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.

  7. #7
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422
    Originally posted by sbasak
    In VB6, array was always passed by reference. That mean in VB.NET, we can pass an array both by value & by reference. isn't it??
    Apparently not.
    I just found out the hard way that my
    Code:
    (ByVal SideLengths() As Single)
    is being returned modified from the sub.

    That ain't right. DaveBo
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  8. #8
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,763
    Passing an array by value is fairly absurd in theory, since function arguments are generally pushed onto the stack (though maybe not in .NET). DaveBo's cryptic post suggests that arrays are passed by ref as default behavior, which makes sense to be, though it would be inconsistent.

    How would you pass an array ByVal? If I really wanted to, I'd make a copy, then pass the reference to the copy as the argument, but .NET will do all that under the covers. Since everything is an object, copying then passing a pointer seems like a likely method of implementing ByVal, and it would work just as well on arrays as intrinsic types.
    My usual boring signature: Nothing

  9. #9
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    re. my cryptic post

    I was simply passing an array of 3 singles into a sub, I asked it to be passed ByVal and VB never complained so I assumed I was getting what I asked for,
    i.e. that VB would make the local array copy for me "under the covers" in the sub.

    But my program was failing miserably because VB was lying to me.

    I repeat - that ain't right.
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  10. #10
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    I think the point is: You can choose if a valuetype has to be passed byval or byref. A reference type is always passed byref, also if compiler doesn't correct your 'byval'.
    Live long and prosper (Mr. Spock)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.