473,473 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

use byval for object in method signature

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
3 2356
"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
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
=?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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matthias Klöpper | last post by:
Hi, I have a little problem with supplying a mixed set of byVal and byRef parameters to a dynamically created PInvoke method. I have the following message signature which works perfectly when...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
14
by: Robin Tucker | last post by:
Although I've been working on this project for 8 months now, I'm still not sure of the difference between ByVal and ByRef. As most objects in VB are reference types, passing ByVal I've discovered...
14
by: Niklas | last post by:
Hi What I have learned is that a variable is just a reference when dealing with Objects. Are you supposed to use ByVal or ByRef in functions? They produce the same result or have I missed...
5
by: william | last post by:
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...
0
by: Travis Oliphant | last post by:
This post is to gather feedback from the wider community on PEP 357. It is nearing the acceptance stage and has previously been discussed on python-dev. This is a chance for the wider Python...
2
by: Bob | last post by:
Hi, I'm still new to asp.net and i would like to understand this: "Source As Object, e As EventArgs" <script runat="server"> Sub button1(Source As Object, e As EventArgs) p1.InnerHtml="You...
6
by: LordHog | last post by:
Hello all, I recently ran into a strange behavior which I don't understand. I have two 'Add' method which a slightly different signature which they look like public void Add( string varName,...
1
by: vishnu | last post by:
Hi, I am working on asp.net project which I converted the code fron VB to C# and instead of RaiseEvent in VB code I used the following code. using System; using System.Data; using...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.