print barcode with vb.net The is and as Operators | in Visual C#.NET

Encode Code 128 Code Set B in Visual C#.NET The is and as Operators |

The is and as Operators |
Code 128 Creation In Visual C#.NET
Using Barcode maker for Visual Studio .NET Control to generate, create Code128 image in VS .NET applications.
www.OnBarcode.com
Decode Code 128 Code Set B In C#.NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
namespace Example_13_3_ _ _ _is_and_as { interface IStorable { void Read( ); void Write(object obj); int Status { get; set; } } interface ICompressible { void Compress( ); void Decompress( ); } public class Note : IStorable { private string myString; public Note(string theString) { myString = theString; } public override string ToString( ) { return myString; } #region IStorable public void Read( ) { Console.WriteLine("Executing Note's Read Method for IStorable"); } public void Write(object o) { Console.WriteLine("Executing Note's Write Method for IStorable"); } public int Status { get; set; } #endregion // IStorable } public class Document : Note, ICompressible {
Print 2D Barcode In C#.NET
Using Barcode creator for .NET framework Control to generate, create Matrix 2D Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
UPC A Encoder In Visual C#.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Universal Product Code version A image in .NET applications.
www.OnBarcode.com
|
Barcode Printer In C#
Using Barcode maker for Visual Studio .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
EAN-13 Supplement 5 Creation In C#.NET
Using Barcode maker for .NET framework Control to generate, create GTIN - 13 image in .NET framework applications.
www.OnBarcode.com
13: Interfaces
GS1-128 Creator In Visual C#
Using Barcode generator for .NET framework Control to generate, create EAN / UCC - 14 image in Visual Studio .NET applications.
www.OnBarcode.com
EAN / UCC - 8 Generator In Visual C#.NET
Using Barcode creator for VS .NET Control to generate, create UPC - 8 image in VS .NET applications.
www.OnBarcode.com
private int documentID; public int ID { get { return this.documentID; } } public Document(string docString, int documentID) : base(docString) { this.documentID = documentID; } #region ICompressible public void Compress( ) { Console.WriteLine("Executing Document's Compress Method for ICompressible"); } public void Decompress( ) { Console.WriteLine("Executing Document's Decompress Method for ICompressible"); } #endregion // ICompressible } // end Document class
Creating Code 128 Code Set B In None
Using Barcode encoder for Software Control to generate, create Code 128 Code Set B image in Software applications.
www.OnBarcode.com
Code-128 Maker In None
Using Barcode generator for Online Control to generate, create Code 128 Code Set A image in Online applications.
www.OnBarcode.com
class Tester { public void Run( ) { string testString = "String "; Note[] myNoteArray = new Note[3]; for (int i = 0; i < 3; i++) { string docText = testString + i.ToString( ); if (i % 2 == 0) { Document myDocument = new Document( docText, (i + 5) * 10); myNoteArray[i] = myDocument; } else { Note myNote = new Note(docText); myNoteArray[i] = myNote; } }
Creating Barcode In .NET
Using Barcode creation for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
Creating UCC.EAN - 128 In Java
Using Barcode generator for Android Control to generate, create GS1 128 image in Android applications.
www.OnBarcode.com
The is and as Operators |
PDF417 Generation In None
Using Barcode drawer for Office Word Control to generate, create PDF-417 2d barcode image in Word applications.
www.OnBarcode.com
Encoding Data Matrix ECC200 In None
Using Barcode creator for Font Control to generate, create Data Matrix image in Font applications.
www.OnBarcode.com
foreach (Note theNote in myNoteArray) { Console.WriteLine("\nTesting {0} with IS", theNote); theNote.Read( ); // all notes can do this if (theNote is ICompressible) { ICompressible myCompressible = theNote as ICompressible; myCompressible.Compress( ); } else { Console.WriteLine("This storable object is not compressible."); } if (theNote is Document) { Document myDoc = theNote as Document; // clean cast myDoc = theNote as Document; Console.WriteLine("my documentID is {0}", myDoc.ID); } } foreach (Note theNote in myNoteArray) { Console.WriteLine("\nTesting {0} with AS", theNote); ICompressible myCompressible = theNote as ICompressible; if (myCompressible != null) { myCompressible.Compress( ); } else { Console.WriteLine("This storable object is not compressible."); } // end else Document theDoc = theNote as Document; if (theDoc != null) { Console.WriteLine("My documentID is {0}", ((Document)theNote).ID); } else { Console.WriteLine("Not a document."); }
USS Code 39 Maker In .NET Framework
Using Barcode printer for ASP.NET Control to generate, create Code 39 Extended image in ASP.NET applications.
www.OnBarcode.com
UPC-A Supplement 5 Creator In Java
Using Barcode drawer for Android Control to generate, create UPC Symbol image in Android applications.
www.OnBarcode.com
|
Paint PDF 417 In Java
Using Barcode creation for Java Control to generate, create PDF417 image in Java applications.
www.OnBarcode.com
Recognize Barcode In Java
Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications.
www.OnBarcode.com
13: Interfaces
Code 128A Printer In .NET
Using Barcode creator for Reporting Service Control to generate, create Code 128 Code Set B image in Reporting Service applications.
www.OnBarcode.com
Read Barcode In VS .NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
} } static void Main( ) { Tester t = new Tester( ); t.Run( ); } // end class Tester
} }
The output looks like this:
Testing String 0 with IS Executing Note's Read Method for IStorable Executing Document's Compress Method for ICompressible my documentID is 50 Testing String 1 with IS Executing Note's Read Method for IStorable This storable object is not compressible. Testing String 2 with IS Executing Note's Read Method for IStorable Executing Document's Compress Method for ICompressible my documentID is 70 Testing String 0 with AS Executing Document's Compress Method for ICompressible My documentID is 50 Testing String 1 with AS This storable object is not compressible. Not a document. Testing String 2 with AS Executing Document's Compress Method for ICompressible My documentID is 70
The best way to understand this program is to take it apart piece by piece. Within the namespace, you declare two interfaces, IStorable and ICompressible, and then two classes: Note, which implements IStorable; and Document, which derives from Note (and thus inherits the implementation of IStorable) and which also implements ICompressible. Finally, you add the class Tester to test the program. Within the Run( ) method of the Tester class, you create an array of Note objects, and you add to that array two Document instances and one Note instance. You use the counter i of the for loop as a control if i is even, you create a Document object; if it s odd, you create a Note.
The is and as Operators |
You then iterate through the array, extracting each Note in turn, and use the is operator to test first whether the Note can safely be assigned to an ICompressible reference:
if (theNote is ICompressible) { ICompressible myCompressible = theNote as ICompressible; myCompressible.Compress( ); } else { Console.WriteLine("This storable object is not compressible."); }
If it can, you cast theNote to ICompressible, and call the Compress( ) method. Then you check whether the Note can safely be cast to a Document:
if (theNote is Document) { Document myDoc = theNote as Document; // clean cast myDoc = theNote as Document; Console.WriteLine("my documentID is {0}", myDoc.ID); }
In the case shown, these tests amount to the same thing, but you can imagine that you could have a collection with many types derived from Note, some of which implement ICompressible and some of which do not. You can use the interim variable as we ve done here:
myDoc = theNote as Document; Console.WriteLine( "my documentID is {0}", myDoc.ID );
Or, you can cast and access the property all in one ugly but effective line, as you do in the second loop:
Console.WriteLine( "My documentID is {0}", ( ( Document ) theNote ).ID );
The extra parentheses are required to ensure that the cast is done before the attempt at accessing the property. The second foreach loop uses the as operator to accomplish the same work, and the results are identical. (The second foreach loop actually generates less intermediate language code, and thus is slightly more efficient.)
Copyright © OnBarcode.com . All rights reserved.