How to: Convert an Object to Another Type in Visual Basic
You convert an Object
variable to another data type by using a conversion keyword such as CType Function.
Example
The following example converts an Object
variable to an Integer
and a String
.
Public Sub objectConversion(ByVal anObject As Object)
Dim anInteger As Integer
Dim aString As String
anInteger = CType(anObject, Integer)
aString = CType(anObject, String)
End Sub
If you know that the contents of an Object
variable are of a particular data type, it is better to convert the variable to that data type. If you continue to use the Object
variable, you incur either boxing and unboxing (for a value type) or late binding (for a reference type). These operations all take extra execution time and make your performance slower.
Compile the code
This example requires:
- A reference to the System namespace.
See also
Phản hồi
https://aka.ms/ContentUserFeedback.
Sắp ra mắt: Trong năm 2024, chúng tôi sẽ dần gỡ bỏ Sự cố với GitHub dưới dạng cơ chế phản hồi cho nội dung và thay thế bằng hệ thống phản hồi mới. Để biết thêm thông tin, hãy xem:Gửi và xem ý kiến phản hồi dành cho