Create an extension method in Visual Studio .NET

Creating PDF 417 in Visual Studio .NET Create an extension method

Create an extension method
PDF-417 2d Barcode Creation In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create PDF 417 image in ASP.NET applications.
www.OnBarcode.com
Barcode Maker In VS .NET
Using Barcode printer for ASP.NET Control to generate, create bar code image in ASP.NET applications.
www.OnBarcode.com
1. In Visual Studio 2008, open the ExtensionMethod project, located in the \Microsoft Press\Visual CSharp Step by Step\ 12\ExtensionMethod folder in your Documents folder. 2. Display the Util.cs le in the Code and Text Editor window. This le contains a static class named Util in a namespace named Extensions. The class is empty apart from the // to do comment. Remember that you must de ne extension methods inside a static class.
PDF 417 Creator In Visual C#.NET
Using Barcode creator for .NET framework Control to generate, create PDF-417 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Creating PDF-417 2d Barcode In VS .NET
Using Barcode encoder for .NET framework Control to generate, create PDF417 image in VS .NET applications.
www.OnBarcode.com
12
Create PDF417 In VB.NET
Using Barcode creation for .NET Control to generate, create PDF417 image in VS .NET applications.
www.OnBarcode.com
Data Matrix 2d Barcode Generator In .NET
Using Barcode drawer for ASP.NET Control to generate, create ECC200 image in ASP.NET applications.
www.OnBarcode.com
Working with Inheritance
European Article Number 13 Drawer In .NET
Using Barcode creation for ASP.NET Control to generate, create GTIN - 13 image in ASP.NET applications.
www.OnBarcode.com
QR Code Creation In .NET Framework
Using Barcode creator for ASP.NET Control to generate, create Quick Response Code image in ASP.NET applications.
www.OnBarcode.com
3. Add a public static method to the Util class, named ConvertToBase. The method should take two parameters: an int parameter named i, pre xed with the this keyword to indicate that the method is an extension method for the int type, and another ordinary int parameter named baseToConvertTo. The method will convert the value in i to the base indicated by baseToConvertTo. The method should return an int containing the converted value. The ConvertToBase method should look like this:
Encoding Bar Code In .NET
Using Barcode printer for ASP.NET Control to generate, create barcode image in ASP.NET applications.
www.OnBarcode.com
Encoding UCC-128 In .NET Framework
Using Barcode printer for ASP.NET Control to generate, create GS1 128 image in ASP.NET applications.
www.OnBarcode.com
static class Util { public static int ConvertToBase(this int i, int baseToConvertTo) { } }
Matrix 2D Barcode Generator In Visual Studio .NET
Using Barcode printer for ASP.NET Control to generate, create 2D Barcode image in ASP.NET applications.
www.OnBarcode.com
USPS PLANET Barcode Creator In Visual Studio .NET
Using Barcode maker for ASP.NET Control to generate, create Planet image in ASP.NET applications.
www.OnBarcode.com
4. Add an if statement to the ConvertToBase method that checks that the value of the baseToConvertTo parameter is between 2 and 10. The algorithm used by this exercise does not work reliably outside this range of values. Throw an ArgumentException with a suitable message if the value of baseToConvertTo is outside this range. The ConvertToBase method should look like this:
Printing UPCA In Java
Using Barcode printer for BIRT reports Control to generate, create UPC-A image in BIRT applications.
www.OnBarcode.com
Bar Code Drawer In Objective-C
Using Barcode drawer for iPhone Control to generate, create bar code image in iPhone applications.
www.OnBarcode.com
public static int ConvertToBase(this int i, int baseToConvertTo) { if (baseToConvertTo < 2 || baseToConvertTo > 10) throw new ArgumentException( Value cannot be converted to base + baseToConvertTo.ToString()); }
European Article Number 13 Printer In Java
Using Barcode encoder for Java Control to generate, create GS1 - 13 image in Java applications.
www.OnBarcode.com
Create EAN / UCC - 14 In None
Using Barcode encoder for Font Control to generate, create EAN / UCC - 13 image in Font applications.
www.OnBarcode.com
5. Add the following statements shown in bold to the ConvertToBase method, after the statement that throws the ArgumentException. This code implements a well-known algorithm that converts a number from base 10 to a different number base:
Drawing Bar Code In Visual Basic .NET
Using Barcode generation for .NET Control to generate, create barcode image in VS .NET applications.
www.OnBarcode.com
USS Code 128 Recognizer In C#
Using Barcode reader for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
public static int ConvertToBase(this int i, int baseToConvertTo) { ... int result = 0; int iterations = 0; do { int nextDigit = i % baseToConvertTo; result += nextDigit * (int)Math.Pow(10, iterations); iterations++; i /= baseToConvertTo; } while (i != 0); return result; }
Draw European Article Number 13 In None
Using Barcode printer for Software Control to generate, create UPC - 13 image in Software applications.
www.OnBarcode.com
Draw Quick Response Code In .NET
Using Barcode creation for .NET Control to generate, create QR image in .NET applications.
www.OnBarcode.com
6. Display the Program.cs le in the Code and Text Editor window.
Part II
Understanding the C# Language
7. Add the following using statement after the using System; statement at the top of the le:
using Extensions;
This statement brings the namespace containing the Util class into scope. The ConvertToBase extension method will not be visible in the Program.cs le if you do not perform this task. 8. Add the following statements to the Entrance method of the Program class:
int x = 591; for (int i = 2; i <= 10; i++) { Console.WriteLine( {0} in base {1} is {2} , x, i, x.ConvertToBase(i)); }
This code creates an int named x and sets it to the value 591. (You could pick any integer value you want.) The code then uses a loop to print out the value 591 in all number bases between 2 and 10. Notice that ConvertToBase appears as an extension method in IntelliSense when you type the period (.) after x in the Console.WriteLine statement. 9. On the Debug menu, click Start Without Debugging. Con rm that the program displays messages showing the value 591 in the different number bases to the console, like this:
10. Press Enter to close the program. Congratulations. You have successfully used inheritance to de ne a hierarchy of classes, and you should now understand how to override inherited methods and implement virtual methods. You have also seen how to add an extension method to an existing type.
12
Working with Inheritance
If you want to continue to the next chapter: Keep Visual Studio 2008 running, and turn to 13. If you want to exit Visual Studio 2008 now: On the File menu, click Exit. If you see a Save dialog box, click Yes (if you are using Visual Studio 2008) or Save (if you are using Visual C# 2008 Express Edition) and save the project.
Copyright © OnBarcode.com . All rights reserved.