Need Help Computing Volume of Cylinder

  • Thread starter Thread starter Basil Fawlty
  • Start date Start date
B

Basil Fawlty

Hi everyone, I have an assignment, to create a simple VB program that
computes the volume of a cylinder. The Form is pretty simple, it has a
label and text box for base radius, another for height and another for
volume with an OK button. I have the code to put into the OK button (Which
I've done):

Private Sub OK_Click( ) --not this line
r = Val(radius.Text) --input this line
h = Val(hght.Text) --input this line
pi = 22 / 7 --input this line
v = pi * (r ^ 2) * h --input this line
volume.Text= Str$(v) --input this line
End Sub --not this line


When I go to run the Build, it complains I have not declared the variables
that you see above. Where do I goto do that? I assume under the label or
text box for the base radius, and so forth, but which one? What do I key
into them that is syntactically correct to match the code I was given to
use?

I am using VB.NET 2003 SE. Please let me know if I need to post more code,
and where I would get it from to post.

My many thanks for any assistance.
 
Try something like this ( Untested )

Private Sub OK_Click( ) --not this line
dim r as double = Val(radius.Text) --input this line
dim h as double= Val(hght.Text) --input this line
dim v as double =MATH.PI * (r ^ 2) * h --input this line
volume.Text= Str$(v) --input this line
End Sub --not this line
 
Ok, I just did that, and when I build, the errors are a lot less which is
great, it now say the radius, hght and volume are not declared. So clearly
they must be, but declared in what way? To hold a empty slot and await for
user input? I don't think this would be a dim. Sorry to be a dense dork,
but I need more help. My many thanks for the help you have already
provided.
 
Basil,

That is because it is an English Car, the steering wheel is on the other
side you know.

However when you set those textboxes on your form, it will probably go
better.

Cor
 
Ok, what do you mean by set the textboxes? You know, I only know how to run
a B&B... not this programming stuff
 
Basil,
Ok, what do you mean by set the textboxes? You know, I only know how to
run a B&B... not this programming stuff
This is a newsgroup about programming.

Cor
 
Basil,

Click right on those textboxes you have draged on your form and gives them
in the properties what you open by clicking on that text, the "name" as
have called them in the code.

Cor
 
Ok, did that, for the 3 text boxes, not the ok button that doesthe compute,
yet I still get the same build error, which is:
"text is not a member of single, name hght is not declared and name volume
is not declared." Here is the latest code behind the ok button at this
point:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim r As Single, h As Single, pi As Single, v As Single, radius As Single

r = Val(radius.Text)

h = Val(hght.Text)

pi = 22 / 7

v = pi * (r ^ 2) * h

volume.Text = Str$(v)

End Sub
 
Which means that radius is not a control in this scope and does not have a
Text property.

You defined it as a single in your button code. If you also have a control
called radius, then this is not seen inside the button code scope. This is
called a hole in the scope.


HTH
 
Below is where I have the code now, I do a Build with no errors, and then go
to Debug, Start and the program pops up, I key in a radius, then height
number, and click ok, it does nothing. And, maybe due to the mode I am
running the program, the text boxes all have text like radius.Text and such
in them.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim r As Single, h As Single, pi As Single, v As Single, radius As Single,
hght As Single, volume As Single

r = Val(radius)

h = Val(hght)

pi = 22 / 7

v = pi * (r ^ 2) * h

volume = Str$(v)

End Sub
 
OK, I think we are a little disconnected here Basil.


The missing part of the picture you seem to have is that the controls on the
form have identifier names which you use to access them. For example, if you
dray a textBox control onto the forms design canvass. It would normally
receive a name of TextBox1. This is the instance name for this instance of
the TextBox class.

One of the members is Text, this is the value which is displayed in the
textbox. There is a default value normally the same name as the instance
name ( TextBox1 ) in my example.

In your button code, you have DIM'd some variables like radius etc. But the
dont seem to be connected to the values in the controls on your form. So,
lets assume you have a control for the radius value called radiusTextBox.

In your code you can ither reference this text value directly or assign it
to a local Variable For example.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim r As Single, h As Single, pi As Single, v As Single, radius As Single,
hght As Single, volume As Single

r = Val(radiusTextBox.Text)

What you did was to this I think

r = Val(radius)

This has the affect of returning the name of the control to the 'r' local
variable.

Your button code should look something similar to this.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim vol, len, rad As Double

len = Convert.ToDouble(Me.lengthTextBox.Text)

rad = Convert.ToDouble(Me.radiusTextBox.Text)

vol = len * Math.PI * rad ^ 2

Me.volumeLabel.Text = vol.ToString("0.##")

End Sub


HTH
 
Sorry

//
r = Val(radius)

This has the affect of returning the name of the control to the 'r' local
variable.
//

Should have read.

This has the affect of returning the name of the local radius variable to
the 'r' local variable, which is confused really.
 
I'm sorry, but your going over my head. What do I change based on the below
being my code at this time below? I'm lost as I do not know where to make
the text box changes to bring about the connection to grab input for the
program to work in properties, on the form screen, hand mod the code, if so
which textbox pre-keyed in do I mod?????? I'm just too new to this.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim r As Single, h As Single, pi As Single, v As Single

r = Val(radius.text)

h = Val(hght.text)

pi = 22 / 7

v = pi * (r ^ 2) * h

volume.text = Str$(v)

End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged

End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox3.TextChanged

End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox2.TextChanged

End Sub

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Label1.Click

End Sub

Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Label2.Click

End Sub

Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Label3.Click

End Sub

End Class
 
I got it to run, I was going to the wrong place to mod the text box name in
the properties. What is the best way to run the program, Iaftera good build
I go to Debug and Start, isn't there a better or alternate way? Also, I see
text in the textboxes, why? Will that go away if I run it not in the IDE?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim r As Single, h As Single, pi As Single, v As Single

r = Val(radius.Text)

h = Val(hght.Text)

pi = 22 / 7

v = pi * (r ^ 2) * h

volume.Text = Str$(v)

End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles radius.TextChanged

End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles volume.TextChanged

End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles hght.TextChanged

End Sub

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Label1.Click

End Sub

Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Label2.Click

End Sub

Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Label3.Click

End Sub

End Class
 
r = Val(radius.text)

^^^^^^is good as long as your textbox for radius is called 'radius' and you
DONT have a local variable in your button code called the same name
 
The text in the text boxes are default values. If you dont want it clear it
from the properties box.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top