Lesson 4: Converting Between Types in VB.NET

Drawer QR Code in VB.NET Lesson 4: Converting Between Types

Lesson 4: Converting Between Types
Creating QR-Code In Visual Basic .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create QR-Code image in Visual Studio .NET applications.
www.OnBarcode.com
Scanning Quick Response Code In VB.NET
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Table 1-7
Draw Bar Code In Visual Basic .NET
Using Barcode generation for Visual Studio .NET Control to generate, create bar code image in .NET framework applications.
www.OnBarcode.com
Barcode Decoder In Visual Basic .NET
Using Barcode scanner for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Methods for Explicit Conversion
Generating QR Code JIS X 0510 In C#
Using Barcode generator for Visual Studio .NET Control to generate, create Denso QR Bar Code image in VS .NET applications.
www.OnBarcode.com
Create QR Code 2d Barcode In .NET
Using Barcode printer for ASP.NET Control to generate, create Quick Response Code image in ASP.NET applications.
www.OnBarcode.com
System Type System.Convert
Denso QR Bar Code Creator In VS .NET
Using Barcode printer for .NET framework Control to generate, create QR Code JIS X 0510 image in VS .NET applications.
www.OnBarcode.com
Encoding Barcode In VB.NET
Using Barcode maker for VS .NET Control to generate, create barcode image in .NET applications.
www.OnBarcode.com
Visual Basic
Matrix Barcode Creation In Visual Basic .NET
Using Barcode creation for .NET framework Control to generate, create Matrix Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
GTIN - 128 Drawer In Visual Basic .NET
Using Barcode drawer for VS .NET Control to generate, create UCC-128 image in .NET framework applications.
www.OnBarcode.com
Converts Between types that implement the System.IConvertible interface.
Generate UCC - 12 In VB.NET
Using Barcode generator for Visual Studio .NET Control to generate, create UPC-A Supplement 5 image in VS .NET applications.
www.OnBarcode.com
Painting ISSN - 10 In Visual Basic .NET
Using Barcode generation for VS .NET Control to generate, create ISSN - 13 image in .NET framework applications.
www.OnBarcode.com
CType type.ToString, type.Parse
USS Code 128 Drawer In .NET
Using Barcode drawer for Reporting Service Control to generate, create Code-128 image in Reporting Service applications.
www.OnBarcode.com
2D Barcode Maker In .NET
Using Barcode printer for ASP.NET Control to generate, create Matrix 2D Barcode image in ASP.NET applications.
www.OnBarcode.com
(type) cast operator
Generate UPC Symbol In None
Using Barcode printer for Software Control to generate, create UPC-A Supplement 5 image in Software applications.
www.OnBarcode.com
Making GTIN - 13 In Java
Using Barcode encoder for Java Control to generate, create EAN13 image in Java applications.
www.OnBarcode.com
Between types that define conversion operators. Between string and base types; throws an exception if the conversion is not possible. From string to a base type; returns false if the conversion is not possible.
GTIN - 12 Creator In None
Using Barcode maker for Font Control to generate, create UPC A image in Font applications.
www.OnBarcode.com
Creating QR Code In .NET Framework
Using Barcode creation for Visual Studio .NET Control to generate, create QR image in .NET framework applications.
www.OnBarcode.com
type.TryParse, type.TryParseExact CBool, CInt, CStr, etc.
Barcode Drawer In .NET Framework
Using Barcode drawer for Reporting Service Control to generate, create bar code image in Reporting Service applications.
www.OnBarcode.com
Barcode Reader In C#
Using Barcode decoder for .NET framework Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Between base Visual Basic types; compiled inline for better performance. (Visual Basic only.) Between types. DirectCast throws an exception if the types are not related through inheritance or if they do not share a common interface; TryCast returns Nothing in those situations. (Visual Basic only.)
DirectCast, TryCast
Narrowing conversions may return an incorrect result if the source value exceeds the destination type s range. If a conversion between the types is not defined, you receive a compile-time error.
1
Framework Fundamentals
What Are Boxing and Unboxing
Boxing converts a value type to a reference type, and unboxing converts a reference type to a value type. The following example demonstrates boxing by converting an int (a value type) to an object (a reference type):
' VB Dim i As Integer = 123 Dim o As Object = CType(i, Object) // C# int i = 123; object o = (object)i;
Unboxing occurs if you assign a reference object to a value type variable. The following example demonstrates unboxing:
' VB Dim o As Object = 123 Dim i As Integer = CType(o, Integer) // C# object o = 123; int i = (int)o;
Boxing Tips
Boxing and unboxing incur overhead, so you should avoid them when programming intensely repetitive tasks. Boxing also occurs when you call virtual methods that a structure or any value type inherits from System.Object, such as ToString. Follow these tips to avoid unnecessary boxing: Implement type-specific versions (overloads) for a procedure that must be able to accept various value types. It is better to create several overloaded procedures than one procedure that accepts an Object argument. Use generics whenever possible instead of coding arguments of the object type. Override the ToString, Equals, and GetHash virtual members when defining structures.
How to Implement Conversion in Custom Types
You can define conversions for your own types in several ways. Which technique you choose depends on the type of conversion you want to perform, as follows: Define conversion operators to simplify narrowing and widening conversions between numeric types.
Lesson 4: Converting Between Types
Override ToString to provide conversion to strings, and override Parse to provide conversion from strings. Implement System.IConvertible to enable conversion through System.Convert. Use this technique to enable culture-specific conversions. Implement a TypeConverter class to enable design-time conversion for use in the Properties window of Visual Studio. Design-time conversion is outside the scope of the exam, and the TypeConverter class is not covered in this book.
MORE INFO
Design-Time Conversion
For more information about design-time conversion, read Extending Design-Time Support at http://msdn.microsoft.com/en-us/library/37899azc.aspx.
Defining conversion operators allows you to assign from a value type directly to your custom type. Use the Widening/implicit keyword for conversions that don t lose precision; use the Narrowing/explicit keyword for conversions that could lose precision. For example, the following structure defines operators that allow assignment to and from integer values (note the bold keywords):
' VB Structure TypeA Public Value As Integer ' Allows implicit conversion from an integer. Public Shared Widening Operator CType(ByVal arg As Integer) As TypeA Dim res As New TypeA res.Value = arg Return res End Operator ' Allows explicit conversion to an integer. Public Shared Narrowing Operator CType(ByVal arg As TypeA) As Integer Return arg.Value End Operator ' Provides string conversion (avoids boxing). Public Overrides Function ToString() As String Return Me.Value.ToString End Function End Structure // C# struct TypeA { public int Value;
1
Framework Fundamentals
// Allows implicit conversion from an integer. public static implicit operator TypeA(int arg) { TypeA res = new TypeA(); res.Value = arg; return res; } // Allows explicit conversion to an integer. public static explicit operator int(TypeA arg) { return arg.Value; } // Provides string conversion (avoids boxing). public override string ToString() { return this.Value.ToString(); } }
The preceding type also overrides ToString to perform the string conversion without boxing. Now you can assign integers to a variable of this type directly, as shown here:
' VB Dim a As TypeA, i As Integer ' Widening conversion is OK implicit. a = 42 ' Rather than a.Value = 42 ' Narrowing conversion in VB does not need to be explicit. i = a ' Narrowing conversion can be explicit. i = CInt(a) ' Rather than i = a.Value ' This syntax is OK, too. i = CType(a, Integer) Console.WriteLine("a = {0}, i = {1}", a.ToString, i.ToString) // C# TypeA a; int i; // Widening conversion is OK implicit. a = 42; // Rather than a.Value = 42 // Narrowing conversion must be explicit in C#. i = (int)a; // Rather than i = a.Value Console.WriteLine("a = {0}, i = {1}", a.ToString(), i.ToString());
To implement the System.IConvertible interface, add the IConvertible interface to the type definition. Then use Visual Studio to implement the interface automatically. Visual Studio inserts member declarations for 17 methods, including GetTypeCode, ChangeType, and ToType methods for each base type. You don t have to implement every method, and some such as ToDateTime probably are invalid. For invalid methods, simply throw an exception in C#, Visual Studio automatically adds code to throw an exception for any conversion methods you don t implement.
Copyright © OnBarcode.com . All rights reserved.