Use byval and byref

Source: Internet
Author: User
Use byval and byref

Byval only transmits parameters.

If byref defines a parameter pointing to it, it will pass the value back.

Not very clear!

In Visual Basic, you can specify keywords by specifyingByvalOrByref, Pass the parameter to the process by value or by reference. Passing parameters through values means that the process cannot be calledCodeModify the content of the basic variable element of the parameter. You can use the same method to modify the content of the variable element by referencing the transfer procedure.

The difference between passing parameters by value and by reference is different from dividing the data type into value type and reference type. However, the two categories do affect each other.

If you use keywordsByrefBy referencing and passing variable parameters, the process can modify the variable itself. Specifically, if the parameter is an object variable, you can assign it a new object. This type of reallocation will also affect the callProgramObject variable.

In the example provided by passing the byval parameter, if the replace subroutine is changed to get the parameter array by referencing, the new array K () will be allocated to the array Variable N in the call code. The result of N () is as follows:

Msgbox ("after Replace (n) taking a () argument byref:" & _ CSTR (N (0) & CSTR (N (1 )) & CSTR (N (2) & CSTR (N (3) 'The element values of N () are now 101,201, and 301. because N was 'passed byref, replace cocould modify the Variable N and assign a new' array to it. because N is a reference type, replace cocould also change its members

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.