Closer Look: Converting from One Variable Type to Another
As explained in previous lessons, variables come in different types. The type determines what kind of data a variable can hold. An Integer variable can hold only numeric data without decimal points. A String variable can hold only text.
What happens when you want to display an Integer in a TextBox control that requires a String? The answer is that the data must be converted from one type to another. In this topic, you will explore how to convert data from one type to another, and you will learn some techniques used for data conversion, as well as some of its common pitfalls.
Every variable in Visual Basic can be converted to text by using a special function called CStr (which is shorthand for Convert to String). This function, as the name implies, returns the data represented by the variable as a String. The following procedure demonstrates a simple example of converting an Integer to text.
To convert a variable to text
On the File menu, click New Project.
In the New Project dialog box, in the Templates pane, click Windows Application.
In the Name box, type Conversion and then click OK.
A new Windows Forms project opens.
Double-click the form to open the Code Editor.
In the Form1_Load event handler, type the following code.
This code declares an Integer variable called anInteger, assigns it a value of 54, and then converts that value to text and displays it in a message box by calling theCStr function.
Press F5 to build and run your application. A message box that reads 54 is displayed.
Let's try something just for fun. In the Code Editor, change the line that reads MsgBox(CStr(anInteger)) to read MsgBox(anInteger), and press F5 to run. What happens? The program behaves exactly as it did before. Visual Basic is smart enough to know that what you really want is to convert the Integer to text to display in the message box. However, you cannot rely on this behavior for all cases—there are many variable types that cannot be automatically converted. Therefore, it is good practice to always use the CStr function, even if a variable would automatically be converted to text.
In addition to converting Integer variables to text, the CStr function can be used on any numeric data type, such as Double or Long. It can also be used to convert the Date and Boolean data types to text. For more information on data types, see Closer Look: Data Types.
As you learned in the arithmetic lesson, sometimes the result of an arithmetic operation can't be expressed as an Integer. Just as Visual Basic has a function for converting numbers to text, it also has functions for converting variables from one numeric data type to another. For example, you can use the CDbl (Convert to Double) function in an arithmetic operation to return a fractional number when working with Integer variables. The following procedure shows how to use the CDbl function when dividing two integers.
To convert numeric data types
In the Code Editor, delete the code that you entered in the previous procedure and type the following:
This code declares two Integer variables (A and B), assigns them values of 1 and 2, and then converts the result of the division operation (A / B) using the CDbl function and displays it in a message box.
Press F5 to build and run your application. A message box that reads 0.5 is displayed.
Visual Basic has functions for other types of numeric variables as well. For example, if you add two variables of the type Double and want to round the result to the nearest whole number, use the CInt function. Other numeric conversion functions include CByte, CDec, CLng, and CShort. For a list of all the Visual Basic conversion functions, see Type Conversion Functions (Visual Basic).
In this lesson, you learned how to convert numeric variables to text, and also how to convert between different types of numeric variables. In the next lesson, Comparisons: Using Expressions to Compare Values, you will learn how to evaluate expressions.