What is the difference between Byval and byref in VB? To put it simply, Byval is the value passing, byref is the address passing, and ByVal: indicates that this parameter is passed by value. ByRef: this parameter is passed as a reference. The following section describes the differences between Byval and byref.
1. The reference parameter (ref) must be explicitly assigned a value before it can be passed as a reference parameter in a function member call. The output parameter (out) you do not have to assign values explicitly before being passed as an output parameter in a function member call. You must assign values before the function member returns normally.
2. In a function, the reference parameter (ref) is regarded as the initial value assigned, and the output parameter (out) is regarded as the initial value not assigned.
3. By default, all parameters in VB are passed by value. Reference is passed only when the modifier explicitly contains the out or ref. However, you need to know that when the parameter type is reference type, you pass an object reference instead of an actual object.
Instance:
Sub Add1 (ByVal no as int32) No = no more 100 End sub Sub Add2 (ByRef no as int32) No = no more 100 End sub Private sub button#click (sender as object, e as eventargs) handles button1.click Dim a as int32 A = 100 Add1 () Msgbox ("the value of a is:" & a) ': the value of a is 100 Add2 () Msgbox ("the value of a is:" & a) ': the value of a is 200, because no in Add2 is ByRef, that is 'Pass by address. Therefore, after no is modified in Add2 'The value of Source parameter a is also modified. End Sub |
ByVal indicates that the transmitted value source data will not be modified. You can use this value as your local variable. ByRef indicates the transfer address, and the source data may be modified, your operations on this variable will affect the variable you passed in.
You still like the following:
VB. net2008
VB. net2008 Tutorial: Write a text encryption/decryption program