make barcode with vb.net 8: Inside Methods in Visual C#

Drawing Code 128B in Visual C# 8: Inside Methods

8: Inside Methods
USS Code 128 Creator In C#.NET
Using Barcode printer for VS .NET Control to generate, create Code 128 Code Set B image in VS .NET applications.
www.OnBarcode.com
Recognize Code 128 In C#.NET
Using Barcode scanner for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Passing Value Types by Reference
GS1 - 13 Creation In Visual C#.NET
Using Barcode creator for .NET Control to generate, create EAN / UCC - 13 image in VS .NET applications.
www.OnBarcode.com
GS1 128 Drawer In Visual C#
Using Barcode generator for VS .NET Control to generate, create GTIN - 128 image in VS .NET applications.
www.OnBarcode.com
As we discussed in 7, C# divides the world of types into value types and reference types. All intrinsic types (such as int and long) are value types. Instances of classes (objects) are reference types. When you pass a value type (such as an int) into a method, a copy is made. When you make changes to the parameter, you re actually making changes to the copy. Back in the Run( ) method, the original integer variables first and second are unaffected by the changes made in DoubleInt( ). What you need is a way to pass in the integer parameters by reference so that changes made in the method are made to the original object in the calling method. When you pass an object by reference, the parameter refers to the same object. Thus, when you make changes in DoubleInt( ), the changes are made to the original variables in Run( ). You do this by prefacing the parameters with the keyword ref.
Paint UPC Code In C#
Using Barcode printer for .NET framework Control to generate, create UPC A image in .NET framework applications.
www.OnBarcode.com
Encode Barcode In Visual C#.NET
Using Barcode encoder for VS .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
Technically, when you pass a reference type, the reference itself is passed by value; but the copy that is made is a copy of a reference, and thus that copy points to the same (unnamed) object on the heap as did the original reference object. That is how you achieve the semantics of pass by reference in C# using pass by value. However, that s all behind-the-scenes stuff, and it s acceptable to say that you re passing objects by reference.
QR Code 2d Barcode Creation In Visual C#
Using Barcode creation for .NET framework Control to generate, create QR Code image in .NET applications.
www.OnBarcode.com
USPS Confirm Service Barcode Encoder In C#.NET
Using Barcode creation for Visual Studio .NET Control to generate, create USPS PLANET Barcode image in VS .NET applications.
www.OnBarcode.com
This requires two small modifications to the code in Example 8-3. First, change the parameters of the DoubleInt( ) method to indicate that the parameters are ref (reference) parameters:
Code 128B Drawer In Objective-C
Using Barcode generator for iPad Control to generate, create Code 128 image in iPad applications.
www.OnBarcode.com
Encode Code 128A In Java
Using Barcode creation for Java Control to generate, create Code 128 Code Set C image in Java applications.
www.OnBarcode.com
public void DoubleInt(ref int firstNum, ref int secondNum)
Barcode Decoder In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Create Linear 1D Barcode In .NET Framework
Using Barcode printer for .NET framework Control to generate, create Linear image in VS .NET applications.
www.OnBarcode.com
Second, modify the call to DoubleInt( ) to pass the arguments as references:
Scanning GS1 - 12 In .NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Code 39 Extended Encoder In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create Code 39 image in ASP.NET applications.
www.OnBarcode.com
d.DoubleInt(ref first, ref second);
Encoding EAN128 In Java
Using Barcode encoder for Android Control to generate, create GS1 128 image in Android applications.
www.OnBarcode.com
2D Generation In Visual Basic .NET
Using Barcode creation for Visual Studio .NET Control to generate, create Matrix 2D Barcode image in VS .NET applications.
www.OnBarcode.com
If you leave out the second step of marking the arguments with the keyword ref, the compiler will complain that the argument cannot be converted from an int to a ref int.
GTIN - 12 Drawer In Java
Using Barcode drawer for BIRT reports Control to generate, create UPC-A Supplement 5 image in BIRT applications.
www.OnBarcode.com
EAN / UCC - 14 Creation In None
Using Barcode creator for Microsoft Excel Control to generate, create UCC - 12 image in Office Excel applications.
www.OnBarcode.com
These changes are shown in Example 8-4.
Print ANSI/AIM Code 128 In .NET Framework
Using Barcode creator for ASP.NET Control to generate, create Code128 image in ASP.NET applications.
www.OnBarcode.com
European Article Number 13 Reader In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
using using using using System; System.Collections.Generic; System.Linq; System.Text;
namespace Example_8_4_ _ _ _Passing_by_Reference
Returning Multiple Values |
{ class Doubler { public void DoubleInt(ref int firstNum, ref int secondNum) { firstNum = firstNum * 2; secondNum = secondNum * 2; } } class Tester { public void Run( ) { int first = 5; int second = 10; Console.WriteLine("Before doubling:"); Console.WriteLine("First number: {0}, Second number: {1}", first, second); Doubler d = new Doubler( ); d.DoubleInt(ref first, ref second); Console.WriteLine("After doubling:"); Console.WriteLine("First number: {0}, Second number: {1}", first, second); } static void Main(string[] args) { Tester t = new Tester( ); t.Run( ); } } }
This time, the output looks like this:
Before doubling: First number: 5, Second number: 10 After doubling: First number: 10, Second number: 20
These results are more like what you d expect. By declaring these parameters to be ref parameters, you instruct the compiler to pass them by reference. Instead of a copy being made, the parameters in DoubleInt( ) are references to the corresponding variables (first and second) that were created in Run( ). When you change these values in DoubleInt( ), the change is reflected in Run( ). Keep in mind that ref parameters are references to the actual original value it is as though you said, Here, work on this one. Conversely, value parameters are copies it is as though you said, Here, work on one just like this.
|
8: Inside Methods
out Parameters and De nite Assignment
As we noted in 4, C# imposes definite assignment, which requires that all variables be assigned a value before they are used. Suppose you have a method for returning all three parameters from a Box object. You d call the method something like this:
myBox.GetDimensions( ref myLength, ref myWidth, ref myHeight);
Because of definite assignment, though, you d have to initialize those three variables before you can pass them to your method:
int myLength = 0; int myWidth = 0; int myHeight = 0; myBox.GetDimensions( ref myLength, ref myWidth, ref myHeight);
It seems silly to initialize these values, because you immediately pass them by reference into GetDimensions( ) where they ll be changed; but if you don t initialize them, the compiler will raise an error for each of the variables. C# provides the out modifier for situations such as this, in which initializing a parameter is only a formality. The out modifier removes the requirement that a reference parameter be initialized. The parameters to GetDimension( ), for example, provide no information to the method; they are simply a mechanism for getting information out of it. Thus, by marking all three as out parameters using the out keyword, you eliminate the need to initialize them outside the method. Within the called method, the out parameters must be assigned a value before the method returns. Here are the altered parameter declarations for GetDimensions( ):
public void GetDimensions( out int theLength, out int theWidth out int theHeight ) { theLength = length; theWidth = width; theHeight = height; }
Here is the new invocation of the method in Run( ):
int myLength; int myWidth; int myHeight; myBox.GetDimensions( out myLength, out myWidth, out myHeight);
The keyword out implies the same semantics as the keyword ref, except that it also allows you to use the variable without first initializing it in the calling method.
Copyright © OnBarcode.com . All rights reserved.