Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I think I know the answer to this , but just want to double check. Using OOP like a good boy should, I pass forms to other forms often. By default vs.net uses a ByVal word instead of byref. Also, I've seen many other vb.net code examples that pass a form to a form or an object around and ByVal seems to work like ByRef in those instances.

 

Am i right? I sure hope i'm not passing a whole copy of the form over when i use ByVal, and I hope i'm just passing a reference to the form i want to pass. ByVal should actually mean that i'm passing a copy to the new form though, according to old C++, am i right?

 

So are ByVal and ByRef pretty meaningless on many objects such as forms, controls, and other classes besides pure data values?

Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Posted (edited)

  Stack               Heap
--------------      -----------
| string b;  | ---> | "Hello" |
| int a = 9; |      -----------
--------------

<a> is a value type (the value is on the stack) <b> is a reference type (or pointer, somewhat) to an value on the heap. When <a> or <b> is used ByVal a copy of the value on the stack is used as a parameter. When it is used ByRef, a pointer to <a> or <b> is used. So a ByRef pointer to a heap object is a pointer to a pointer, and you can change what the reference points to.

 

For example

Sub Main
   Dim s As String = "Hello" ' s is on the heap
   Dim i As Integer = 10     ' i is on the stack

   Change (s, i)
   Use (s, i)
End Sub

Sub Change (ByRef rs As String, ByRef ri As Integer)
  rs = "World" ' pointer to local s, changing it to point to the "world" on the heap
  ri = 14        ' pointer to local i, changing the value on the stack to 14
End Sub

Sub Use (ByVal vs As String, ByVal vi As Integer)
  Console.WriteLine(vs) ' is a copy of the pointer to the heap
  Console.WriteLine(vi) ' is a copy of the value
End Sub

Edited by IceAzul
Posted

Ok thanks for the details. I'll stick with ByVal for heap objects.

 

  Stack               Heap
--------------      -----------
| string b;  | ---> | "Hello" |
| int a = 9; |      -----------
--------------

<a> is a value type (the value is on the stack) <b> is a reference type (or pointer, somewhat) to an value on the heap. When <a> or <b> is used ByVal a copy of the value on the stack is used as a parameter. When it is used ByRef, a pointer to <a> or <b> is used. So a ByRef pointer to a heap object is just a pointer to a pointer.

Currently Using: Visual Basic.Net 2005, .Net Framework 2.0

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...