Sunday, April 27, 2008

Check Type of Variable in VB.Net

Identify Variable Type in VB.NET

Sub Check_Variable_Types()

Dim varByte As SByte

Dim varInt As Integer

Dim varString As String

Dim varExp As Exception

varString = "Temp"

varExp = New System.Exception("Sample Exception")

Dim varArr As Object() = {varByte, varInt, varString, varExp}

'The Object data type can point to data of any data type, including any object instance your application recognizes. Use Object when you do not know at compile time what data type the variable might point to.

'The default value of Object is Nothing (a null reference).

MsgBox(varArr.GetType.ToString())

For Each obj As Object In varArr

MsgBox(obj.GetType.IsValueType) 'Gets a value indicating whether the Type is a value type.

MsgBox(obj.GetType().ToString)

Next

End Sub

' Value types are those that are represented as sequences of bits; value types are not classes or interfaces. These are referred to as "structs" in some programming languages. Enums are a special case of value types.

'This property returns false for the ValueType class.

'This property returns true for enumerations, but not for the Enum type itself. For an example that demonstrates this behavior, see IsEnum.

'This property is read-only.


1 comment: