Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

150 Part II Programming Fundamentals


This type of error is called a run-time error—an error that surfaces not during the design
and compilation of the program but later, when the program is running and encounters
a condition that it doesn’t know how to process. If this seems odd, you might imagine
that Visual Basic is simply offering you a modern rendition of the robot plea “Does not
compute!” from the best science-fiction films of the 1950s. The computer-speak message
“Conversion from string ‘birth’ to type ‘Double’ is not valid” means that the words you
entered in the text boxes (“birth” and “day”) could not be converted, or cast, by Visual
Basic to variables of the type Double. Double types can contain only numbers—period.
As we shall explore in more detail later, Visual Studio doesn’t leave you hanging with
such a problem, but provides a dialog box with different types of information to help
you resolve the run-time error. For now, you have learned another important lesson
about data types and when not to mix them.


  1. Click the Stop Debugging button on the Standard toolbar to end the program.


Your program ends and returns you to the development environment.

Note In Chapter 8, “Debugging Visual Basic Programs,” you’ll learn about debugging
mode, which allows you to track down the defects, or bugs, in your program code.

Now take a look at the program code to see how variables were declared and how the
advanced operators were used.


  1. Scroll to the code at the top of the Code Editor, if it is not currently visible.


You see the following comment and program statement:

'Declare FirstNum and SecondNum variables
Dim FirstNum, SecondNum As Double
As you might recall from the previous exercise, FirstNum and SecondNum are the
variables that hold numbers coming in from the TextBox1 and TextBox2 objects.


  1. Change the data type from Double to String so that you can properly test how the
    string concatenation (&) operator works.

  2. Scroll down in the Code Editor to see how the advanced operators are used in the
    program code.
    You see the following code:


'Assign text box values to variables
FirstNum = TextBox1.Text
SecondNum = TextBox2.Text

'Determine checked button and calculate
If RadioButton1.Checked = True Then
TextBox3.Text = FirstNum \ SecondNum
End If
Free download pdf