Creating a volume calculator
Posted on 2006-04-07
I am trying to learn Visual basic and am trying to follow a tutorial to calculate the volume of a cylinder.
I have followed the tutorial but when I run the program I get an error message
saying no Data or method found . I have the interface and all the boxes and labels are as instructed.
The tutorial is below without the picture of the interface. am I failing to follow it precise or is it a typo in the tutorial.
Lesson 2. A program to calculate the Volume of a Cylinder
This program is a simple program that calculates the volume of a cylinder. Let design the interface:
First of all, go to the properties window and change the form caption to Volume of Cylinder, then drag and insert three labels into the form and change their captions to Base Radius, height and volume respectively. After that, insert three Text Boxes and clear its text contents so that you get three empty boxes. Named the text boxes as radius, hght (we cannot use height as it is the built-in control name of VB) and volume respectively. Lastly, insert a command button and change its caption to O.K and its name to OK. Now save the project as cylinder.vbp and the form as cylinder.vbp as well
Now we shall attempt to write the codes for the cylinder program where the interface is shown in Figure 2.8. First of all, name the textbox as radius, hght, and volume. To get the values of the various textboxes, use Val(radius.text), Val(hght.Text) and assign them to the variables r and h. In addition, assign the value 22/7 to the variable pi. After that, write the equation v = pi * (r ^ 2) * h to compute the value of volume of cylinder and then assign it to the variable v. Finally, display the value in the volume textbox using the function Str$.
Private Sub OK_Click( )
r = Val(radius.Text)
h = Val(hght.Text)
pi = 22 / 7
v = pi * (r ^ 2) * h
When you run the program, you should be able to see the interface as shown in Figure 2.8. If you enter a value each in the radius box and the height box, then click OK; the value of the Volume will be displayed in the volume box.
Its when I click OK is were I get an error, saying no data or method found.