ByVal and ByRef in Visual Basic .NET

Encoder QR Code ISO/IEC18004 in Visual Basic .NET ByVal and ByRef
541 ByVal and ByRef
QR-Code Drawer In VB.NET
Using Barcode encoder for VS .NET Control to generate, create QR Code ISO/IEC18004 image in Visual Studio .NET applications.
A parameter acts as a way of assigning a name, or an alias to an input value for use within the subroutine That saves us having to use variables with the same names as the ones de ned as parameter names in the subroutine de nition, which would be a serious restriction on the use of a sub or function Since the two parameters in Listing 538 are passed by value (ByVal), we can be sure that no matter what is done with them inside the function, there will be no effect on any variable that is passed as an input they are input-only parameters The function simply returns a value calculated from them, eg Listing 539
Bar Code Creator In Visual Basic .NET
Using Barcode printer for .NET Control to generate, create bar code image in VS .NET applications.
5 n Controlling Program Code
QR Code Encoder In Visual C#
Using Barcode generator for VS .NET Control to generate, create QR-Code image in Visual Studio .NET applications.
Sub Main() Dim p As Decimal = 50 ConsoleWriteLine("Discounted price: {0}", _ DiscountedPrice(p, 10)) ConsoleWriteLine("Original price: {0}", p) End Sub Listing 539: A ByVal parameter is not changed by a subroutine
Create Denso QR Bar Code In VS .NET
Using Barcode drawer for ASP.NET Control to generate, create QR Code JIS X 0510 image in ASP.NET applications.
Here, the variable p will not be affected, and so the second WriteLine() statement will show that p still has a value of 50 What this demonstrates is that when a parameter is passed ByVal (the default), it cannot be affected by the routine it is passed to This is a very desirable feature as a default, since we can be sure that a subroutine with ByVal parameters will not have any side-effects caused by variables being changed inadvertently We can always say that ByVal parameters are input parameters Parameters that are passed by reference (ByRef) can be affected by the subroutine they are passed to Because of this, ByRef is not the default type of parameter By using a ByRef parameter, we are indicating that this parameter will act as an output from the subroutine it is de ned for For example, consider this alternative way of calculating a discounted price (Listing 540)
QR-Code Generator In Visual Studio .NET
Using Barcode generation for VS .NET Control to generate, create QR image in .NET applications.
Sub ApplyDiscount(ByRef Price As Decimal, _ ByVal DiscountPercent As Single) Price -= Price * DiscountPercent / 100 End Sub Listing 540: A ByRef parameter can be changed by a subroutine
Generate Barcode In Visual Basic .NET
Using Barcode generator for .NET framework Control to generate, create bar code image in .NET applications.
Note that in this case, the Price parameter is passed ByRef (it is necessary to type ByRef when the rst line of the sub is entered, otherwise the default, ByVal, will be used) The DiscountPercent parameter is still de ned as a ByVal parameter, since there is no intention to change this value, either inside or outwith the sub Effectively, Price has been de ned as an input/output parameter, while DiscountPercent is purely an input parameter When the value in Price is changed by subtracting the speci ed discount (a percentage of Price), this will affect the value in any variable passed into the Price parameter To use this routine, simply call it passing the price that is to be updated and the discount rate:
Bar Code Generator In Visual Basic .NET
Using Barcode creator for VS .NET Control to generate, create barcode image in .NET framework applications.
ApplyDiscount(p, 10) ConsoleWriteLine("Discounted price: {0}", p)
ANSI/AIM Code 39 Creator In VB.NET
Using Barcode generator for VS .NET Control to generate, create Code 3 of 9 image in .NET applications.
In the above call, whatever value was in p before the subroutine call will have been reduced by 10% once the subroutine has executed In this case, p has acted as both an input to and an output from the sub Parameters are a general purpose way of sending variables to subroutines, almost as if each parameter was an envelope that could be attached to a subroutine call Some parameters (those passed by value using the ByVal keyword) act as ways
Code 128 Creation In Visual Basic .NET
Using Barcode printer for .NET Control to generate, create Code 128 Code Set A image in .NET applications.
54 Subs, Functions and Parameters
ECC200 Printer In VB.NET
Using Barcode generation for .NET framework Control to generate, create ECC200 image in .NET framework applications.
of inputting a value to a sub or function so that it has data to work on These envelopes are simply deposited into the subroutine so the data can be worked on Others (those passed by reference, using the ByRef keyword) can also take on new values assigned to them within the sub or function, and so act as carriers for data passed back from the routine These envelopes are returned to the sender once the subroutine has nished executing, with the updated versions of the parameters on-board Parameters can seem complex at rst, but just keep in mind the following simple rules
Barcode Drawer In Visual Studio .NET
Using Barcode creation for .NET framework Control to generate, create barcode image in Visual Studio .NET applications.
Encode EAN / UCC - 13 In Java
Using Barcode maker for Java Control to generate, create GTIN - 128 image in Java applications.
Painting Data Matrix 2d Barcode In .NET
Using Barcode maker for ASP.NET Control to generate, create ECC200 image in ASP.NET applications.
Print Bar Code In Java
Using Barcode generator for Java Control to generate, create bar code image in Java applications.
UCC - 12 Maker In Java
Using Barcode printer for Java Control to generate, create Universal Product Code version A image in Java applications.
Data Matrix ECC200 Creator In Visual C#.NET
Using Barcode creator for .NET framework Control to generate, create Data Matrix image in .NET framework applications.