Results 1 to 3 of 3

Thread: [RESOLVED] byVal method headers

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    230

    Resolved [RESOLVED] byVal method headers

    Why if you pass an object as byVal into a method is it then treated as if it were byRef? I know you're only passing the reference across when you pass an object variable but why doesn't VB force you to decalre the header as byRef?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: byVal method headers

    The behavior that you are seeing is internally consistent. In both cases, if you pass ByVal, you get a copy of what is in the variable. If you pass ByRef, then you get a reference to the contents of the variable.

    The key is that a variable that is a reference type only holds the address (reference) of the actual object, whereas a variable that is a value type holds the actual value.

    Take these two points together and you see that the behavior is consistent. Pass a value type ByVal and you get a copy of what is in the variable, which is the actual value. Pass a reference type ByVal, and you get a copy of what is in the variable, but in this case the variable only holds a reference to begin with, so you get a copy of that reference.

    Pass a value type ByRef and you get a reference to that variable, which means you get a reference to the value. Pass a reference type ByRef and you get a reference to the variable, but in this case the variable only holds a reference, so you end up with a reference to a reference.

    A reference to a reference to an object is NOT the same as a reference to an object, though in most cases they would appear to act the same. The difference really comes in when you create a new object. Pass in a reference to a value type variable, and you can use that to change the value of the calling variable. Pass in a reference type either way and you can change the value of the calling variable. However, you can also create an entirely new reference type variable and swap it for the existing one. Had you passed the reference type ByVal, and you could change that variable all you wanted, but you couldn't exchange it for a totally new object.

    Why would you want to do that? You probably wouldn't. However, the situation, as it currently stands, is internally consistent. Both value types and reference types behave the same way as far as the calling convention is concerned. The differences arise due to the differences between value types and reference types, and nothing more.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    230

    Re: byVal method headers

    Quote Originally Posted by Shaggy Hiker View Post
    The behavior that you are seeing is internally consistent. In both cases, if you pass ByVal, you get a copy of what is in the variable. If you pass ByRef, then you get a reference to the contents of the variable.

    The key is that a variable that is a reference type only holds the address (reference) of the actual object, whereas a variable that is a value type holds the actual value.

    Take these two points together and you see that the behavior is consistent. Pass a value type ByVal and you get a copy of what is in the variable, which is the actual value. Pass a reference type ByVal, and you get a copy of what is in the variable, but in this case the variable only holds a reference to begin with, so you get a copy of that reference.

    Pass a value type ByRef and you get a reference to that variable, which means you get a reference to the value. Pass a reference type ByRef and you get a reference to the variable, but in this case the variable only holds a reference, so you end up with a reference to a reference.

    A reference to a reference to an object is NOT the same as a reference to an object, though in most cases they would appear to act the same. The difference really comes in when you create a new object. Pass in a reference to a value type variable, and you can use that to change the value of the calling variable. Pass in a reference type either way and you can change the value of the calling variable. However, you can also create an entirely new reference type variable and swap it for the existing one. Had you passed the reference type ByVal, and you could change that variable all you wanted, but you couldn't exchange it for a totally new object.

    Why would you want to do that? You probably wouldn't. However, the situation, as it currently stands, is internally consistent. Both value types and reference types behave the same way as far as the calling convention is concerned. The differences arise due to the differences between value types and reference types, and nothing more.
    Thanks for your explanation SH. I never thought of it like that but this makes perfect sense now.

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