Conversion Classes & CultureInfo Class in VB.NET

In this article I will explain you about the Conversion Classes & CultureInfo Class in VB.NET
  • 3154

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

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.