|
-
Mar 13th, 2005, 12:41 PM
#1
Thread Starter
New Member
Compute Volume of Cylinder - Simple Program
Hi everyone, private newbie reporting for duty. Below is the code I speak of. When I go to run the Build, it complains I have not declared the variables radius, hght and volume. I know it must go into this same section of code and not with the text boxes that the user would inut from. But I dont think it would be a DIM (I could be wrong), I'm not sure what it would be or what is syntactically correct or exactly where to put it. As in, I need heeelp!
Private Sub OK_Click( )
dim r as double = Val(radius.Text)
dim h as double= Val(hght.Text)
dim v as double =MATH.PI * (r ^ 2) * h
volume.Text= Str$(v)
End Sub
What I have is a simple form with 3 lables and text boxes, one for radius, height and volume and one botton that is mrked OK and has the code above input into it.
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.
-
Mar 13th, 2005, 12:53 PM
#2
Re: Compute Volume of Cylinder - Simple Program
Have you actually renamed the textboxes themselves? or just the labels next to them?
May sound like a silly question but I'm doing that all the time at the moment
I don't live here any more.
-
Mar 13th, 2005, 01:06 PM
#3
Re: Compute Volume of Cylinder - Simple Program
I've done that too, and here's a simple way to prevent it:
Instead of volume.text, use me.volume.text. If after the me., volume is not one of the options, then you have something wrong.
I do go abit further, and wart all the control names. Thus a textbox called volume would actually be called tbVolume. This also helps, because you type it in as tbvolume, and if it does not convert to tbVolume, then you didn't change the name of the control. One little capitalized letter in all variable declarations can save you some headache.
Oh yeah, and always use Option Explicit. I believe you can set that under Tools|Options to be always on. You definitely want it to be always on.
My usual boring signature: Nothing
 
-
Mar 13th, 2005, 01:19 PM
#4
Thread Starter
New Member
Re: Compute Volume of Cylinder - Simple Program
 Originally Posted by wossname
Have you actually renamed the textboxes themselves? or just the labels next to them?
May sound like a silly question but I'm doing that all the time at the moment 
ok, me being rather dense, this means I double click each of the text boxes, and in this case, I see this....
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
And it was the text box for radius in this case, so I am to change the TextBox1_ to radius_ or am I wrong?
-
Mar 13th, 2005, 01:27 PM
#5
Thread Starter
New Member
Re: Compute Volume of Cylinder - Simple Program
 Originally Posted by basil_fawlty
ok, me being rather dense, this means I double click each of the text boxes, and in this case, I see this....
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
And it was the text box for radius in this case, so I am to change the TextBox1_ to radius_ or am I wrong?
I just tried doing that by editing the name in the properties box, then I double clicked the textbox and it didn't change in there. So I guess I must hand change that... I just want to make sure of what I'm doing do I don't make a huge mess of the code. Thanks
-
Mar 13th, 2005, 02:01 PM
#6
Thread Starter
New Member
Re: Compute Volume of Cylinder - Simple Program
Here is the latest code behind the ok button:
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
And I gone to the properties of the 3 text boxes and made the TEXT to same the same name as the code, as in TextBox1 is now radius.Text. I still get the same errors in the build: "Text not a member of single, name hght is not declared and name volume is not declared."
It seems to be so close now.
-
Mar 13th, 2005, 03:23 PM
#7
Thread Starter
New Member
Re: Compute Volume of Cylinder - Simple Program
 Originally Posted by basil_fawlty
Here is the latest code behind the ok button:
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
And I gone to the properties of the 3 text boxes and made the TEXT to same the same name as the code, as in TextBox1 is now radius.Text. I still get the same errors in the build: "Text not a member of single, name hght is not declared and name volume is not declared."
It seems to be so close now.
Ok, I've made more progress, here is my code:
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
Now when I run the program, rather I do a Build, then go to Debug and Start, (maybe there is a better way to "Run"), I get the program box, I key in a radius number, then a height number, and hit ok, nothing happens. And, the textboxes have text in them, doesnt look to slick. Can someone please help me?
-
Mar 13th, 2005, 09:07 PM
#8
PowerPoster
Re: Compute Volume of Cylinder - Simple Program
OK, let's do things the .NET way, you don't want to do all that legacy VB6 stuff 
Name your 3 textboxes:
txtHeight
txtRadius
txtVolume
then put:
at the very top of the project.
And here is the code to make things happen:
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Calculate volume of cylinder
Dim r, h, v As Single
r = Convert.ToSingle(txtRadius.Text) ' Get input from textboxes and convert to Single data type
h = Convert.ToSingle(txtHeight.Text)
v = Convert.ToSingle(Math.PI * (r ^ 2) * h) ' Do the calculation
txtVolume.Text = v.ToString ' Display the result
End Sub ' Button1_Click
Pi is a built in function of the Math class, so you don't need to calculate it yourself.
-
Mar 14th, 2005, 11:00 AM
#9
Re: Compute Volume of Cylinder - Simple Program
Supersparks, you're jumping ahead.
First off, name your controls. This is done by changing the Name property of the control in the form designer screen. Then when you double click on the control, instead of TextBox1_textChanged, you will get the new name_textChanged.
Also, you might be more satisfied with the results of using Decimal rather than Single.
My usual boring signature: Nothing
 
-
Mar 14th, 2005, 01:30 PM
#10
Re: Compute Volume of Cylinder - Simple Program
When using forumula I would recommend Implementing them as functions.
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Lets parse the user input here:
Dim Height as Decimal
Dim Radius as Decimal
Dim Volume as Decimal
Try
Height = Decimal.Parse(txtHeight.Text)
Radius = Decimal.Parse(txtRadius.Text)
'Call the function to get the volume
Volume = VolumeOfCynlinder(Radius, Height)
Msgbox("The Volume of the Cylinder is: " & Volume.ToString)
Catch ex as Exception
Msgbox("Enter Numbers only in the Textboxes")
End Try
End Sub
VB Code:
Public Function VolumeOfCylinder(Radius as Decimal, Height as Decimal) as Decimal
Return (Math.Pi * Radius^2 * Height)
End Function
Now, to use my code you must create two textbox's named txtHeight and txtRadius. You must also add 1 Button named Button1.
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|