answersLogoWhite

0


Best Answer

we can pass parameter in procedure and function two type one is Byval and other is byRef, in byval value Store in stack And byref store in Heap. in a Byval get value a predefind and userdefind by replica and copy of the value. but in byRef program access address of the Variable we can pass parameter in procedure and function two type one is Byval and other is byRef, in byval value Store in stack And byref store in Heap. in a Byval get value a predefind and userdefind by replica and copy of the value. but in byRef program access address of the Variable

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

16y ago

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.
Free Development Management Tool
Define, Enforce & Track your Development and Test efforts to Ship Software OnTime. Download Now

ORCS Web
Trusted by top professionals, like www.ASP.net, ASPAlliance.com, and WindowsForms.net. Come find out what world famous service and support feels like.

Free Development Management Tool
Define, Enforce & Track your Development and Test efforts to Ship Software OnTime. Download Now



Feedbacks about this page from members: - 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


- 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


This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between byval and byref in net?
Write your answer...
Submit
Still have questions?
magnify glass
imp