Hi all,
What is ByVal and ByRef in vb.net.Please explain with examples.
Thanks
What is ByVal and ByRef in vb.net.Please explain with examples.
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Dec 13, 2011, 9:57 PM EST
ByVal:- means passing a copy of a variable to your Subroutine. You can make changes to the copy and the original will not be altered.
ByRef:- means that you are not handing over a copy of the original variable but pointing to the original variable.Here original value will be changed.
Ex:-
Dim x, y As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
x = 20
y = 30
MsgBox("original value=" & x & " , " & y)
change1(x)
change2(y)
MsgBox("changed value=" & x & " , " & y)
End Sub
Sub change1(ByVal x As Integer)
x = x + 10
End Sub
Sub change2(ByRef y As Integer)
y = y + 10
End Sub
Result
original value= 20,30
changed value= 20,40
Thanks