print barcode with vb.net 17: Delegates and Events in Visual C#.NET

Making Code 128B in Visual C#.NET 17: Delegates and Events

17: Delegates and Events
USS Code 128 Creation In Visual C#.NET
Using Barcode maker for VS .NET Control to generate, create Code 128 image in Visual Studio .NET applications.
www.OnBarcode.com
Code 128A Reader In C#
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
{ Console.WriteLine("Simulating playing an audio file here."); audioPlayerStatus = 0; return audioPlayerStatus; } }
Creating Denso QR Bar Code In C#
Using Barcode encoder for .NET framework Control to generate, create QR Code 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Drawing Barcode In Visual C#.NET
Using Barcode creator for .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
This class has one private internal member, and a simple public method that simulates playing an audio file and returning a status code in the form of an int. This method, PlayAudioFile( ), has the signature the delegate requires, so this method can be used with the delegate. (Of course, a real media player would have many more methods than just this one, but we re keeping things simple for testing purposes.) The other media player class is VideoPlayer, with a similar PlayVideoFile( ) method. Within the body of the program, you first need to instantiate the MediaStorage class, and then one of each of the players:
Encoding Barcode In Visual C#.NET
Using Barcode maker for .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
Code128 Printer In Visual C#
Using Barcode generation for .NET Control to generate, create Code 128C image in VS .NET applications.
www.OnBarcode.com
MediaStorage myMediaStorage = new MediaStorage( ); AudioPlayer myAudioPlayer = new AudioPlayer( ); VideoPlayer myVideoPlayer = new VideoPlayer( );
PDF-417 2d Barcode Creator In Visual C#.NET
Using Barcode creation for .NET framework Control to generate, create PDF-417 2d barcode image in VS .NET applications.
www.OnBarcode.com
Making British Royal Mail 4-State Customer Barcode In Visual C#
Using Barcode maker for .NET Control to generate, create British Royal Mail 4-State Customer Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
That s easy enough. The next thing you need to do is instantiate the delegates. The delegates are of the type MediaStorage.PlayMedia (note that you re using the MediaStorage class here, not the object of that class you created a minute ago). You still use the keyword new to instantiate the delegate, but you pass the method PlayAudioFile as a parameter to the delegate when it s created. The result is that audioPlayerDelegate is a delegate of type PlayMedia, which you can now work with as a reference to that method:
Encoding Code 128C In Java
Using Barcode maker for BIRT reports Control to generate, create Code-128 image in Eclipse BIRT applications.
www.OnBarcode.com
Code 128 Code Set A Creation In None
Using Barcode printer for Font Control to generate, create USS Code 128 image in Font applications.
www.OnBarcode.com
// instantiate the delegates MediaStorage.PlayMedia audioPlayerDelegate = new MediaStorage.PlayMedia(myAudioPlayer.PlayAudioFile); MediaStorage.PlayMedia videoPlayerDelegate = new MediaStorage.PlayMedia(myVideoPlayer.PlayVideoFile);
Make Universal Product Code Version A In Objective-C
Using Barcode maker for iPad Control to generate, create UPC A image in iPad applications.
www.OnBarcode.com
Make GTIN - 12 In Java
Using Barcode generation for Java Control to generate, create UPC Code image in Java applications.
www.OnBarcode.com
Now that you have the two delegate instances, you use the delegates with the ReportResult( ) method to see whether the media files were valid. Notice here that what you re passing to the ReportResult( ) method is a reference to a method in a different class altogether:
Code39 Generation In None
Using Barcode creation for Microsoft Word Control to generate, create Code 39 Full ASCII image in Word applications.
www.OnBarcode.com
Making Barcode In .NET
Using Barcode creator for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
myMediaStorage.ReportResult(audioPlayerDelegate); myMediaStorage.ReportResult(videoPlayerDelegate);
Print Code 3 Of 9 In Java
Using Barcode creation for Java Control to generate, create USS Code 39 image in Java applications.
www.OnBarcode.com
PDF-417 2d Barcode Printer In Java
Using Barcode creator for Java Control to generate, create PDF417 image in Java applications.
www.OnBarcode.com
The outcome of this is the first line causes ReportResult( ) to call the PlayAudioFile( ) method, but the second line causes it to call the PlayVideoFile( ) method. At compile time, ReportResult( ) doesn t know which method it is going to call it finds out only when it is invoked at runtime. All it needs to know is that any method it will be asked to call will match the signature defined by the PlayMedia delegate. The full program is shown in Example 17-1, followed by the outcome.
Drawing Barcode In Java
Using Barcode creation for BIRT Control to generate, create Barcode image in BIRT reports applications.
www.OnBarcode.com
Barcode Generator In Visual Basic .NET
Using Barcode creation for .NET framework Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
Delegates |
EAN / UCC - 13 Reader In .NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Universal Product Code Version A Creator In None
Using Barcode drawer for Microsoft Word Control to generate, create UPCA image in Office Word applications.
www.OnBarcode.com
using using using using System; System.Collections.Generic; System.Linq; System.Text;
namespace Example_17_1_ _ _ _Using_Delegates { public class MediaStorage { public delegate int PlayMedia( ); public void ReportResult(PlayMedia playerDelegate) { if (playerDelegate( ) == 0) { Console.WriteLine("Media played successfully."); } else { Console.WriteLine("Media did not play successfully."); } } } public class AudioPlayer { private int audioPlayerStatus; public int PlayAudioFile( ) { Console.WriteLine("Simulating playing an audio file here."); audioPlayerStatus = 0; return audioPlayerStatus; } } public class VideoPlayer { private int videoPlayerStatus; public int PlayVideoFile( ) { Console.WriteLine("Simulating a failed video file here."); videoPlayerStatus = -1; return videoPlayerStatus; } }
|
17: Delegates and Events
public class Tester { public void Run( ) { MediaStorage myMediaStorage = new MediaStorage( ); // instantiate the two media players AudioPlayer myAudioPlayer = new AudioPlayer( ); VideoPlayer myVideoPlayer = new VideoPlayer( ); // instantiate the delegates MediaStorage.PlayMedia audioPlayerDelegate = new MediaStorage.PlayMedia(myAudioPlayer.PlayAudioFile); MediaStorage.PlayMedia videoPlayerDelegate = new MediaStorage.PlayMedia(myVideoPlayer.PlayVideoFile); // call the delegates myMediaStorage.ReportResult(audioPlayerDelegate); myMediaStorage.ReportResult(videoPlayerDelegate); } } class Program { static void Main(string[] args) { Tester t = new Tester( ); t.Run( ); } } }
Just for variety, the video player class returns an error code. Of course, you d probably want your MediaStorage class to take more action than simply reporting that the file didn t play, but we won t go into that here. This is what the output looks like:
Simulating playing an audio file here. Media played successfully. Simulating a failed video file here. Media did not play successfully.
Copyright © OnBarcode.com . All rights reserved.