ARTICLE

The Convert Class : Conversion made easy in VB.NET

Posted by Mahesh Chand Articles | Visual Basic 2010 November 17, 2004
The convert class is used to covert from one data type to another. Most of the common conversions can be done by using this class.
 
Reader Level:

 

Tools Used  Visual VB .NET
Namespace System
Assembly mscorlib.dll

The convert class is used to covert from one data type to another. Most of the common conversions can be done by using this class. Here are it's major members. 

Member Description
IsDBNull Gets a value indicating whether the specified object is of type DBNull.
ToBoolean Overloaded. Converts a value to a Boolean value.
ToByte Overloaded. Converts a value to an 8-bit unsigned integer.
ToChar Overloaded. Converts a value to a unicode character.
ToDateTime Overloaded. Converts a value to a DateTime.
ToDecimal Overloaded. Converts a value to a Decimal.
ToDouble Overloaded. Returns the specified parameter as an 8-byte Double precision floating-point number.
ToInt16 Overloaded. Converts a value to a 16-bit signed integer.
ToInt32 Overloaded. Converts a value to a 32-bit signed integer.
ToInt64 Overloaded. Converts a value to a 64-bit signed integer.
ToSByte Overloaded. Converts a value to an 8-bit signed integer.
ToSingle Overloaded. Converts a value to a single-precision floating point number.
ToString Overloaded. Converts a value to a string.
ToUInt16 Overloaded. Converts a value to a 16-bit unsigned integer.
ToUInt32 Overloaded. Converts a value to a 32-bit unsigned integer.
ToUInt64 Overloaded. Converts a value to a 64-bit unsigned integer.

Accept IsDBNull, all of the members are overloaded. Now smart idea here is that each member converts everything to what it is suppose to. For example, ToString converts a value to string. 

Public Shared ToStringCType(As string, Char) Converts a Char to a String.
Public Shared ToStringCType(As string, Boolean) Returns a String representing the specified number.
Public Shared ToStringCType(As string, System.UInt16) Converts a UInt16 to a String. This method is not CLS-compliant.
Public Shared ToStringCType(As string, Integer) Converts an Int32 to a String.
Public Shared ToStringCType(As string, System.SByte) Converts an SByte to a String. This method is not CLS-compliant.
Public Shared ToStringCType(As string, Byte) Converts a Byte to a String.
Public Shared ToStringCType(As string, Short) Converts an Int16 to a String.
Public Shared ToStringCType(As string, Long) Converts an Int64 to a String.
Public Shared ToStringCType(As string, Decimal) Converts a Decimal to a String.
Public Shared ToStringCType(As string, DateTime) Converts a DateTime to a String.
Public Shared ToStringCType(As string, TimeSpan) Converts a TimeSpan to a String.
Public Shared ToStringCType(As string, single) Returns a String representing the specified number.
Public Shared ToStringCType(As string, System.UInt64) Converts a UInt64 to a String. This method is not CLS-compliant.
Public Shared ToStringCType(As string, Double) Converts a Double to a String.
Public Shared ToStringCType(As string, short,Integer) Converts a 16-bit signed integer to a string in a specified base.
Public Shared ToStringCType(As string, byte,Integer) Converts an 8-bit unsigned integer to a string in a specified base.
Public Shared ToStringCType(As string, long,Integer) Converts a 64-bit signed integer to a string using a specified base.
Public Shared ToStringCType(As string, int,Integer) Converts a 32-bit signed integer to a string using a specified base.

All members of the Convert class are overloaded in the similar manner. Now say you want to convert a double value to a string.

Private d As Double = 12.245.
Private str1 As String = d.ToString().

You use all overloaded functions in the same fashion. No more conversion problems. Neat. I was sick of calling Win APIs to convert these data types.

share this article :
post comment
 

i have a table in ms-access table name <t1> field as <name> text, <Dob> datetime.How to insert record in that field using vb.net

Posted by arun ghadai Mar 01, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor