Data type conversion

Created by akaBot Support, Modified on Tue, 6 Aug, 2024 at 2:58 PM by akaBot Support

Data Type Conversion

Here are functions for converting one data type to another. Common conversion patterns are summarized below.

1. Conversion to String

This is the method for converting data types other than String type to String type for handling strings. It is used when you want to set a value of a data type other than String type for a property value that is String type. For example, when you want to display the result of an operation in the "Input > Text property" of a message box activity.

Data Type
Conversion Method
Example and Result
Int32
Int32.ToString
100.ToString
(variable var is of Int32 type and assigned 100)
Double
Double.ToString
(variable var is of Double type and assigned 3.14)
Boolean
Boolean.ToString
True.ToString
False.ToString
(variable var is of Boolean type and assigned False)
DateTime
DateTime.ToString
(variable var is of DateTime type and has the initial value)
Object
Object.ToString
(variable var is of Object type and assigned 100)
CType(Object, String)
(variable var is of Object type and assigned 100)

2. Conversion to Numeric Types

This is the method for converting data types other than numeric types to numeric types for handling numbers. It is used when performing calculations. If you convert a variable with a non-numeric value to a numeric type, an error will occur. For example, executing "Int32.Parse("100")" or "Int32.Parse("a")" will throw a FormatException stating that the input string is not in the correct format.

Conversion Pattern
Conversion Method
Example and Result
String ⇒ Int32
Int32.Parse(String)
Int32.Parse("100")
String ⇒ Double
Double.Parse(String)
Double.Parse("3.14")
Object ⇒ Int32
CType(Object, Int32)
(variable var is of Object type and assigned 100)
Object ⇒ Double
CType(Object, Double)
(variable var is of Object type and assigned 3.14)

3. Conversion to Other Types

This is the method for converting to data types other than String or numeric types.

Conversion Pattern
Conversion Method
Example and Result
String ⇒ Boolean
Boolean.Parse(String)
Boolean.Parse("True")
String ⇒ DateTime
DateTime.Parse(String)
DateTime.Parse("2020/9/1")
Object ⇒ Boolean
CType(Object, Boolean)
(variable var is of Object type and assigned True)
Object ⇒ DateTime
CType(Object, DateTime)
(variable var is of Object type and assigned "2020/9/1 1:1:1")

These methods and functions allow for flexible handling and conversion of various data types in VB.


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article