ARTICLE

Conversion Classes & CultureInfo Class in VB.NET

Posted by Manish Tewatia Articles | Visual Basic Language August 04, 2010
In this article I will explain you about the Conversion Classes & CultureInfo Class in VB.NET
 
Reader Level:

Conversion Classes

You can convert one data type to another type using the Convert class. The Convert class has a number of static methods, beginning with To and ending with the target data type for example, ToInt32 or ToByte. If successful, the returned instance is an object of the target data type. Not all Strings and Arrays 645 conversions will be possible when using the Convert method, so use a try-catch block if you think you may get uncertain results.

Example of Convert Class:

    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text

    Module ConversionClass
        Sub Main()
            Dim intExample As Int32 = 19
            ' the methods inside convert
            Console.WriteLine("Convert.ToString, result = {0}", Convert.ToString(intExample))
            Console.WriteLine("Convert.ToBoolean, result = {0}", Convert.ToBoolean(intExample))
            'displays True
            Console.WriteLine("Convert.ToByte, result = {0}", Convert.ToByte(intExample))
            Console.WriteLine("Convert.ToChar, result = {0}", Convert.ToChar(intExample))
            Console.WriteLine("Convert.ToDouble, result = {0}", Convert.ToDouble(intExample))
            Console.ReadLine()
        End Sub
    End
Module

Output

1.gif

If you want to convert strings into dates, you can use the Parse method for the DateTime data type or you can use the ToDateTime method. Both achieve the same objective. Because users sometimes incorrectly enter dates, don't forget to include appropriate exception handling to make sure that any conversion errors are caught. If you want to provide more control over parsing a date, use the ParseExact method.

CultureInfo Class

As shown in below example, the CultureInfo class contains cultural information like DisplayName, Calendar, and various official abbreviations.

Example of CultureInfo Class:

    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports System.Globalization

    Module ConversionClass
        Sub Main()
            Dim c As New CultureInfo("tr")
            Console.WriteLine("The CultureInfo is set to: {0}", c.DisplayName)
            Console.WriteLine("The parent culture is: {0}", c.Parent.DisplayName)
            Console.WriteLine("The three leter ISO language name is: {0}", c.ThreeLetterISOLanguageName)
            Console.WriteLine("The default calendar for this culture is: {0}" & vbLf & vbLf, c.Calendar.ToString())
            Console.ReadLine()
        End Sub
    End
Module

Output

2.gif

As demonstrated in below example, the RegionInfo class contains regional information, including DisplayName, currency information, and official abbreviations. RegionInfo also contains a static property to retrieve the CurrentRegion.

Example of RegionInfo Class:

    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports System.Globalization

    Module ConversionClass
        Sub Main()
            Dim r As New RegionInfo("tr")
            Console.WriteLine("The name of this region is: {0}", r.Name)
            Console.WriteLine("The currency symbol for the region is: {0}", r.CurrencySymbol)
            Console.WriteLine("Is this region metric : {0} " & vbLf & vbLf, r.IsMetric)
            Console.ReadLine()
        End Sub
    End
Module

Output

3.gif

Conclusion

Hope this article would have helped you in understanding the Conversion Classes & CultureInfo Class in VB.NET

Login to add your contents and source code to this article
share this article :
post comment
 
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.
Become a Sponsor