C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Working of ByVal & ByRef


Posted Date: 10 Mar 2004    Resource Type: Articles    Category: .NET Framework
Author: Sadha SivamMember Level: Gold    
Rating: Points: 5



The working of ByVal & ByRef in Vb.Net

As we all know we can pass the variable either ByVal or ByRef.

Ok, Let have close look at the following code snippet.

I have class TestClass as shown below


Public Class TestClass
Public str As String = "Hai i am the original...."
End Class


'In the form place a command button(lets call that as Button1) and paste the following code in the form.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tes As New TestClass()
PassByVal(tes)
MsgBox(tes.str)
End Sub
Private Sub PassByVal(ByVal testobj As TestClass)
testobj.str = "String is changed"
End Sub




Now what will be displayed in the message box.

We all know that when a parameter is passed byval a copy of it is sent and whatever changes we make will not affect the original.

So, Now Take some time and tell what will be the output.
------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

If you have say "Hai i am the original...." will be displayed. You are wrong.

If you have chosen correctly, no problem.

Actually it shows "String is changed".

Explanation,

Because, even if you pass an object as a ByVal parameter, it is passed internally as ByRef.

Since we are passing an object of TestClass . Even though the function PassByVal takes it as the ByVal parameter, it will be passed as ByRef.




Responses

Author: Madhu     28 May 2004Member Level: Bronze   Points : 0
I appreciate your idea, it was good but. If you re-initialize the value for an object then the latest value will be available.

If
I =10
I = 20

What could the value of I now

Byval : You cannot change the value
Byref : You can change the value


Your program is misleading because; these two key words will play key roll in changing the parameter value.

If this is Byval this will not happen
byref this will happen

That is the reason Microsoft has changed parameter as ByVal in .NET it was Byref in VB 6.0

I feel that this program gives an impression that there is no difference in Byval and Byref



Author: Madhu     28 May 2004Member Level: Bronze   Points : 0
I appreciate your idea, it was good but. If you re-initialize the value for an object then the latest value will be available.

If
I =10
I = 20

What could the value of I now

Byval : You cannot change the value
Byref : You can change the value


Your program is misleading because; these two key words will play key roll in changing the parameter value.

If this is Byval this will not happen
byref this will happen

That is the reason Microsoft has changed parameter as ByVal in .NET it was Byref in VB 6.0

I feel that this program gives an impression that there is no difference in Byval and Byref



Author: Sadha Sivam    28 May 2004Member Level: Gold   Points : 0
hi madhu,

U have mistaken, i have written what holds good for an object and not for a variable please try that for an object and let me know.

With regards,

Sadha Sivam S



Author: niyazi sanjar    25 Sep 2004Member Level: Bronze   Points : 0
Class Object are always passed byReference in .NET. So shouldnt really matter if it is ByVal or ByRef.


Author: Paul Waldschmidt    16 Oct 2004Member Level: Bronze   Points : 0
You are working with a class object. In .NET, classes are always passed ByRef, unlike value types.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to add a mulitple entries in listbox using an add button from another listbox and then remove th
Previous Resource: Visual Inheritance in VB.Net
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use