asp.net.ph

Skip Navigation LinksHome > Getting Started > ASP.NET Base Types > Converting Types

Converting Types

Getting Started   ASP.NET Base Types


Implicit Conversion

It is possible, under some circumstances, to force the automatic conversion, or coercion, of the value of a given type into a different type. For example, numbers can easily be included in strings.

In expressions involving concatenation of string and numeric values, the framework automatically converts numeric values into strings. The text assigned for a Label control in the below example illustrates this, interspersing the integer variables rowIndex and rowCount, which are set at run time based on user input.

lblTracker.Text = "Record " + rowIndex + " of " + rowCount + 
   " " + lstTypes.SelectedItem + " Home Plans";
  C# VB

 Show me 

This kind of conversion is said to be implicit, as the conversion can safely be implied or performed without any conversion syntax.

NOTE: The set of implicit conversions actually permissible is decided in part by the language in use, and depends basically on whether the conversion is in effect widening or narrowing. Unless you are somehow familiar with which conversions can occur implicitly, it would be wise not to rely on implicit conversion.

More on those in a while.

Explicit Conversion

In cases where implicit conversions are not permissible, a method called casting is used. This kind of conversion is said to be explicit, as this requires the use of language-specific cast expressions.

For instance, the row referenced in the DataView ( myView [ 0 ] ) in our earlier example, is returned as a generic object that must be explicitly cast into the appropriate class type, before access to any of the type’s members ( i.e., properties, methods, events, etc. ) is possible. The program will not compile without the cast.

In that example, the row is first cast into a DataRowView, which then enables access to the DataRowView.Item.

title.InnerText = (  ( DataRowView ) myView [ 0 ] ) [ "model" ].ToString ( );
  C# VB

Casting is also employed in that demo to convert Session key values into their corresponding types.

For instance, the values of rowIndex and rowCount mentioned earlier are actually stored as session variables before each postback. This provides a way to track at which record each user accessing the data is. These values are then retrieved on each subsequent page load as follows.

rowIndex =  ( int ) Session [ "rowIndex" ];
rowCount =  ( int ) Session [ "rowCount" ];
  C# VB

System.Convert Class

In addition to supporting explicit cast conversion, the .NET Framework provides a complete set of methods to convert between all supported types using the System.Convert class. This class enables a language-independent way to perform conversions.

This example shows how the value of a field in a data table is retrieved and at the same time transformed using one of the methods of the System.Convert class. The output of the conversion is used to determine the parameters that are then passed to a method that provides for paging thru the results set.

// get total results from productinfo table
totalresults = Convert.ToInt32 ( ds.Tables [ "ProductInfo" ].Rows [ 0 ] [ "TotalResults" ] );

// determine total page count
pageCount = totalresults % 10 == 0 ? totalresults / 10 : ( totalresults / 10 ) + 1;
pageSetCount = pageCount % 10 == 0 ? pageCount / 10 : ( pageCount / 10 ) + 1;

buildPager ( pageCount, pageSetCount );
  C# VB

 Show me 

Now let’s briefly run thru some type essentials to help better appreciate conversion rules and issues as we take a closer look at the available conversion techniques.

More ...
Back to top


© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note