Differences between Byval and byref in VB (example)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.