print barcode with vb.net Delegates in Visual C#.NET

Creator Code 128 in Visual C#.NET Delegates

Delegates
Printing Code 128B In Visual C#
Using Barcode drawer for .NET framework Control to generate, create Code 128 Code Set B image in Visual Studio .NET applications.
www.OnBarcode.com
Scanning Code-128 In Visual C#
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
A delegate is a reference type, like the other reference types you ve seen in this book, but instead of referring to an object, a delegate refers to a method. This is called encapsulating the method. When you create the delegate, you specify a method signature and return type; you can encapsulate any matching method with that delegate. You create a delegate with the delegate keyword, followed by a return type and the signature of the methods that can be delegated to it, as in the following:
UPC-A Supplement 2 Creator In Visual C#
Using Barcode encoder for .NET Control to generate, create UPC-A Supplement 2 image in .NET framework applications.
www.OnBarcode.com
Paint ANSI/AIM Code 39 In Visual C#
Using Barcode encoder for .NET Control to generate, create Code 3/9 image in .NET applications.
www.OnBarcode.com
public delegate int FindResult(object obj1, object obj2);
Barcode Printer In Visual C#
Using Barcode creator for .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
PDF-417 2d Barcode Maker In C#.NET
Using Barcode generation for Visual Studio .NET Control to generate, create PDF-417 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
This declaration defines a delegate named FindResult, which will encapsulate any method that takes two objects as parameters and that returns an int. Once the delegate is defined, you can encapsulate a member method with that delegate by creating an instance of the delegate, passing in a method that matches the return type and signature. Notice that the delegate has no method body; that s because you re not defining the method here. You re simply saying that this delegate can encapsulate any method with the appropriate signature; you don t care what it does or how it does it, as long as it has the right parameters and returns the correct type. As an alternative, you can use anonymous methods or lambda expressions, as described later in this chapter. In either case, you can use the delegate to invoke that encapsulated method. Delegates decouple the class that declares the delegate from the class that uses the delegate. That s part of the principle of encapsulation that we talked about back in 6. The class that declares the delegate FindResult doesn t need to know how the result is found, or what class uses the delegate; all it needs to do is get an int back. For example, suppose you have a class called MediaStorage that you use to store and manage various media files audio files, video files, animation files; the type of file
2D Barcode Drawer In C#.NET
Using Barcode maker for .NET framework Control to generate, create Matrix image in Visual Studio .NET applications.
www.OnBarcode.com
Encode C 2 Of 5 In C#.NET
Using Barcode creator for VS .NET Control to generate, create Code 2/5 image in .NET applications.
www.OnBarcode.com
Delegates |
Code 128B Decoder In .NET Framework
Using Barcode decoder for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Creating ANSI/AIM Code 128 In Objective-C
Using Barcode drawer for iPad Control to generate, create Code 128 Code Set A image in iPad applications.
www.OnBarcode.com
doesn t matter to the class. Suppose further that you want this class to be able to play the files to make sure they can be played successfully, and report on whether they played properly or not (as a way of testing whether the file is valid). The MediaStorage class doesn t need to know how to play the files; it just needs to receive a code indicating whether the file played successfully or not. You could fulfill this requirement with interfaces, although it may not be worth it to you to define an entirely new interface and create an instance of it when you could use a delegate instead. In this case, we ll be testing only two types of media files, so we ll use delegates. If there were a wider range of media file types, you might want to define an appropriate interface. The delegate declaration in MediaStorage is rather simple:
Data Matrix Generation In VS .NET
Using Barcode creation for Reporting Service Control to generate, create Data Matrix ECC200 image in Reporting Service applications.
www.OnBarcode.com
Painting Code 128A In Java
Using Barcode creator for BIRT reports Control to generate, create ANSI/AIM Code 128 image in BIRT applications.
www.OnBarcode.com
public delegate int PlayMedia( );
DataMatrix Printer In VS .NET
Using Barcode maker for ASP.NET Control to generate, create Data Matrix ECC200 image in ASP.NET applications.
www.OnBarcode.com
PDF-417 2d Barcode Encoder In .NET Framework
Using Barcode printer for Reporting Service Control to generate, create PDF-417 2d barcode image in Reporting Service applications.
www.OnBarcode.com
This delegate takes no parameters, but expects an int as a return value, to indicate whether the file played successfully. A value of 0 indicates success; anything else indicates failure. Note again that the method has no body. The only other method in MediaStorage is ReportResult( ), which outputs to the console the result from the media test:
Barcode Encoder In None
Using Barcode drawer for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
Generate UPC Symbol In .NET
Using Barcode maker for Visual Studio .NET Control to generate, create UCC - 12 image in VS .NET applications.
www.OnBarcode.com
public void ReportResult(PlayMedia playerDelegate) { if (playerDelegate( ) == 0) { Console.WriteLine("Media played successfully."); } else { Console.WriteLine("Media did not play successfully."); } }
Barcode Drawer In VS .NET
Using Barcode generator for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
Drawing Barcode In .NET
Using Barcode printer for .NET framework Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
This looks like a normal method, except for the parameter it takes: playerDelegate, which is not an int, as you might expect, but rather a delegate, of type PlayMedia, which you declared earlier. It s not easy to think of a method in the same terms that you might normally think of an object, but that s how delegates work. In the body of the method, you can t declare playerDelegate directly as an integer, because playerDelegate is a reference to a method. Instead, you evaluate the method that the delegate points to, and compare the result. That s why you re testing playerDelegate( ) == 0. From there, you just output an appropriate message. Take a look now at one of the media player classes:
UPC - 13 Reader In .NET
Using Barcode decoder for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
PDF-417 2d Barcode Maker In None
Using Barcode generation for Office Excel Control to generate, create PDF-417 2d barcode image in Office Excel applications.
www.OnBarcode.com
public class AudioPlayer { private int audioPlayerStatus; public int PlayAudioFile( )
|
Copyright © OnBarcode.com . All rights reserved.