By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,964 Members | 1,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,964 IT Pros & Developers. It's quick & easy.

use byval for object in method signature

P: n/a
Hi everyone,

I just started learning VB.NET, I found there are a lots methods passing object parameter by value. For example,

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

It's impossible in vb6.In vb6, the object is always passed by reference.
How come we can pass object byval in .NET?
Can anyone explain it?
Thanks.

william

Jul 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"william" <an*******@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com
Hi everyone,

I just started learning VB.NET, I found there are a lots methods
passing object parameter by value. For example,

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click

It's impossible in vb6.In vb6, the object is always passed by
reference. How come we can pass object byval in .NET?
Can anyone explain it?


unless I'm very much mistaken about something in .Net it is the same as VB6;
in both cases you are passing the *reference to the object* either byval or
byref. The object itself is not passed in either platform. The ByVal/ByRef
determines only whether the called procedure can change the reference to a
new object or to Nothing.

--
C# newbie... posts are probably inaccurate, inelegant or both

Jul 21 '05 #2

P: n/a
William,
It's impossible in vb6.In vb6, the object is always passed by reference. Wrong!

Unfortunately the Ref word is overloaded here. :-|

An object in both VB6 & VB.NET is a Reference Type!

ByVal & ByRef Parameters are independent of Reference & Value Types. All
parameters in VB.NET by default are passed ByVal, you should only pass a
parameter ByRef when you have to, which is when you need to modify the
callers variable.

A Reference Type is an object that exists on the heap. If I have a variable
that is a reference type and assign the variable to another variable. Both
variables will be pointing to the same object on the heap.

Dim x As Person
x = New Person()
Dim y As Person
y = x

Both x & y are the exact same Person object on the heap.

A Value Type does not live on the Heap. If I have a value type variable and
I assign it to another variable, a copy of the value is made.

Dim x As Integer
x = 100
Dim y As Integer
y = x

Although both x & y have the value 100, they are physically different values
as a copy was made.

Now when you pass a variable to a ByVal parameter a copy of the variable is
made. So for a Reference Type a copy of the reference is made, which means
there is still only one object on the heap & two references to that object.
For a Value Type a copy of the value is made.

When you pass a variable to a ByRef parameter a reference to that variable
is made. So for a Reference Type you have a reference to a reference to the
object, for a Value Type you have a reference to the value.

Remember ByVal & ByRef are how parameters are passed. Reference & Value
Types are how quantities are stored.

Hope this helps
Jay
"william" <an*******@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com... Hi everyone,

I just started learning VB.NET, I found there are a lots methods passing object parameter by value. For example,
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
It's impossible in vb6.In vb6, the object is always passed by reference.
How come we can pass object byval in .NET?
Can anyone explain it?
Thanks.

william

Jul 21 '05 #3

P: n/a
=?Utf-8?B?d2lsbGlhbQ==?= <an*******@discussions.microsoft.com> wrote:
I just started learning VB.NET, I found there are a lots methods
passing object parameter by value. For example,

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click

It's impossible in vb6.In vb6, the object is always passed by reference.
How come we can pass object byval in .NET?
Can anyone explain it?


Have a look at http://www.pobox.com/~skeet/csharp/parameters.html -
it's expressed in C# terms, but I'm sure you'll be able to follow it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.