Windows Phone Developers

Sunday, April 27, 2008

Conversions in Dot Net (.Net) - Strict , Widening and Narrowing



Impact of Option Strict in Conversions (.NET)

Option Strict restricts implicit data type conversions to only widening conversions. Widening conversions explicitly do not permit any data type conversions in which data loss may occur and any conversion between numeric types and strings. For more information about widening conversions, see the Widening Conversions section.

When you use the Option Strict statement, the statement must appear before any other code. In Visual Basic .NET, you can typically convert any data type to any other data type implicitly. Data loss can occur when the value of one data type is converted to a data type with less precision or with a smaller capacity. However, you receive a run-time error message if data will be lost in such a conversion. Option Strict notifies you of these types of conversions at compile time so that you can avoid them.

Option Strict also generates an error message in the following scenarios:

  • For any undeclared variable. This is because Option Strict also implies Option Explicit. (Option Strict On requires all variable declarations to have an 'As' clause - Error ID: BC30209 )

To correct this error

1. Check to see if the As keyword is misspelled.

2. Supply an As clause for the declared variable, or turn Option Strict Off.

  • Late binding.

You will get the following error, if you attempt to execute the code below “Option Strict On disallows implicit conversions from 'Double' to 'Integer'”

Sub Convert_Double_To_int()

Dim DoubleVar As Double

Dim IntVar As Integer

DoubleVar = 123.456

IntVar = 2 + DoubleVar

End Sub




An important consideration with a type conversion is whether the result of the conversion is within the range of the destination data type. A widening conversion changes a value to a data type that can accommodate any possible value of the original data. A narrowing conversion changes a value to a data type that might not be able to hold some of the possible values. The above conversion is an example of narrowing conversion

Widening Conversions

The following table lists the standard widening conversions.

Data Type

Widens to Data Types

Byte

Byte, Short, Integer, Long, Decimal, Single, Double

Short

Short, Integer, Long, Decimal, Single, Double

Integer

Integer, Long, Decimal, Single, Double

Long

Long, Decimal, Single, Double

Decimal

Decimal, Single, Double

Single

Single, Double

Double

Double

Any enumerated type

Its underlying integer type and any type to which it will widen

Char

Char, String

Any type

Object, any interface that it implements

Any derived type

Any base type from which it is derived

Nothing

Any data type or object type


The following conversions may lose precision:

  • Integer to Single
  • Long to Single or Double
  • Decimal to Single or Double

However, these conversions do not lose information or magnitude.

Widening conversions always succeed, and you can always perform widening conversions implicitly.

Explicit Conversion with Casting

An explicit conversion uses a type conversion keyword. Visual Basic .NET or Visual Basic 2005 provides several such keywords, which coerce an expression in parentheses to the data type that you want. These keywords behave as functions, but the compiler generates the code inline. Therefore, execution is a little faster with explicit conversion than with a function call.

The following table lists the available conversion keywords.

Type Conversion Keyword

Converts Expression
to Data Type

Permitted Data Types of Expression to Be Converted

CBool

Boolean

Any numeric type (including Byte and enumerated types), String, Object

CByte

Byte

Any numeric type, any enumerated type, Boolean, String, Object

CChar

Char

String, Object

CDate

Date

String, Object

CDbl

Double

Any numeric type (including Byte and enumerated types), Boolean, String, Object

CDec

Decimal

Any numeric type (including Byte and enumerated types), Boolean, String, Object

CInt

Integer

Any numeric type (including Byte and enumerated types), Boolean, String, Object

CLng

Long

Any numeric type (including Byte and enumerated types), Boolean, String, Object

CObj

Object

Any type

CShort

Short

Any numeric type (including Byte and enumerated types), Boolean, String, Object

CSng

Single

Any numeric type (including Byte and enumerated types), Boolean, String, Object

CStr

String

Any numeric type (including Byte), Boolean, Char, Char array, Date, Object

CType

Type specified following the comma (,)

When you convert to an elementary type (including an array of an elementary type), the same types as are permitted for the corresponding conversion keyword.

When you convert to a composite type, the interfaces it implements and the classes from which it inherits.

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

No comments:

Post a Comment