Results 1 to 10 of 10

Thread: Compute Volume of Cylinder - Simple Program

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    6

    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.

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    6

    Re: Compute Volume of Cylinder - Simple Program

    Quote 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?

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    6

    Re: Compute Volume of Cylinder - Simple Program

    Quote 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

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    6

    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.

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    6

    Re: Compute Volume of Cylinder - Simple Program

    Quote 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?

  8. #8
    PowerPoster SuperSparks's Avatar
    Join Date
    May 2003
    Location
    London, England
    Posts
    265

    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:

    VB Code:
    1. Imports System.Math
    at the very top of the project.

    And here is the code to make things happen:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         '  Calculate volume of cylinder
    3.  
    4.         Dim r, h, v As Single
    5.  
    6.         r = Convert.ToSingle(txtRadius.Text)      ' Get input from textboxes and convert to Single data type
    7.         h = Convert.ToSingle(txtHeight.Text)
    8.         v = Convert.ToSingle(Math.PI * (r ^ 2) * h)   ' Do the calculation
    9.  
    10.         txtVolume.Text = v.ToString        ' Display the result
    11.  
    12.     End Sub     ' Button1_Click

    Pi is a built in function of the Math class, so you don't need to calculate it yourself.
    Nick.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  10. #10
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Re: Compute Volume of Cylinder - Simple Program

    When using forumula I would recommend Implementing them as functions.

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         'Lets parse the user input here:
    3.  
    4.         Dim Height as Decimal
    5.         Dim Radius as Decimal
    6.         Dim Volume as Decimal
    7.        
    8.         Try
    9.              Height = Decimal.Parse(txtHeight.Text)
    10.              Radius = Decimal.Parse(txtRadius.Text)
    11.              'Call the function to get the volume
    12.              Volume = VolumeOfCynlinder(Radius, Height)
    13.  
    14.              Msgbox("The Volume of the Cylinder is: " & Volume.ToString)
    15.         Catch ex as Exception
    16.              Msgbox("Enter Numbers only in the Textboxes")
    17.         End Try
    18.  
    19.     End Sub

    VB Code:
    1. Public Function VolumeOfCylinder(Radius as Decimal, Height as Decimal) as Decimal
    2.  
    3.    Return (Math.Pi * Radius^2 * Height)
    4. 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
  •  



Click Here to Expand Forum to Full Width