Training
Module
Choose the correct data type in your C# code - Training
Choose the correct data type for your code from several basic types used in C#.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
An object variable contains a pointer to data that is stored elsewhere. The type of that data can change during run time. At any moment, you can use the GetTypeCode method to determine the current run-time type, or the TypeOf Operator to find out if the current run-time type is compatible with a specified type.
On the object variable, call the GetType method to retrieve a System.Type object.
Dim myObject As Object
myObject.GetType()
On the System.Type class, call the shared method GetTypeCode to retrieve the TypeCode enumeration value for the object's type.
Dim myObject As Object
Dim datTyp As Integer = Type.GetTypeCode(myObject.GetType())
MsgBox("myObject currently has type code " & CStr(datTyp))
You can test the TypeCode enumeration value against whichever enumeration members are of interest, such as Double
.
Use the TypeOf
operator in combination with the Is Operator to test the object with a TypeOf
...Is
expression.
If TypeOf objA Is System.Windows.Forms.Control Then
MsgBox("objA is compatible with the Control class")
End If
The TypeOf
...Is
expression returns True
if the object's run-time type is compatible with the specified type.
The criterion for compatibility depends on whether the specified type is a class, structure, or interface. In general, the types are compatible if the object is of the same type as, inherits from, or implements the specified type. For more information, see TypeOf Operator.
Note that the specified type cannot be a variable or expression. It must be the name of a defined type, such as a class, structure, or interface. This includes intrinsic types such as Integer
and String
.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Training
Module
Choose the correct data type in your C# code - Training
Choose the correct data type for your code from several basic types used in C#.
Documentation
TypeOf Operator - Visual Basic
Learn more about: TypeOf Operator (Visual Basic)
GetType Operator - Visual Basic
Learn more about: GetType Operator (Visual Basic)
Learn more about: ReadOnly (Visual Basic)