Type Conversion using functions

You can also use the classic Visual Basic keywords such as Val(), CStr(), CInt(), CBool(), and so on to perform data type conversions with the standard data types. However, the CType() function is a nice generic solution that works for all scenarios.

Example

Module Module1

    Sub Main()

        Dim TextString As String = "world"
        Dim TextString2 As String = "111"
        Dim Number As Integer
        Dim Number2 As Integer

        Number = Val(TextString) ' Number is now 0, because TextString contains no numeric information.
        Number2 = Val(TextString2) ' Number is now 0, because TextString contains no numeric information.

        Console.WriteLine(Number)
        Console.WriteLine(Number2)

        Console.ReadLine()
    End Sub

End Module

Output

0
111

Share

You may also like...