Search This Blog

Thursday, July 18, 2013

CType Function Visual Basic NET

VB.NET > Functions > CType

CType function returns the result of explicitly converting an expression to a specified data type.

Example:

Public Class Form1
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim mStr As String = "100"
    Dim mInt As Integer = CType(mStr, Integer) 'convert "100" string to mInt = 100 OK

    mStr = "aaa"
    mInt = CType(mStr, Integer) ' Error: Conversion from string "aaa" to type 'Integer' is not valid. NOK
  End Sub
End Class