Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

VB Problem

Options
  • 09-10-2002 4:42pm
    #1
    Registered Users Posts: 4,560 ✭✭✭


    Trying to write program to calculate volume of cylinder where by
    user enters in either height Or Radius and the program automatically Calculates the Volume.
    I have the main part workin e.g if i enter 10 it calculates it however if i enter in 100 it sends a debug error

    You should assume that H=2R (height = Radius * radius)
    ____________________

    Option Explicit

    Private Sub cmdReset_Click()
    txtHeight.Text = ""
    txtRadius.Text = ""
    txtHeight.Text = 0
    txtRadius.Text = 0

    End Sub

    Private Sub cmdTest_Click()

    Dim HT As Integer
    Dim RS As Integer
    Dim Volume As Integer
    Dim Ans As Integer
    Dim Sum As Integer
    Dim R As Integer
    Dim H As Integer


    If txtHeight.Text > 0 Then
    txtRadius.Visible = False


    RS = txtHeight.Text / 2


    lblRds.Caption = "" & RS
    HT = txtHeight.Text

    Ans = (RS * RS) * HT
    Sum = 0.33 * 3.14
    Volume = Sum * Ans
    lblResult.Caption = "The Volume is " & Volume
    Else
    txtHeight.Visible = False


    HT = txtRadius.Text * 2
    lblhght.Caption = "" & HT
    RS = txtRadius.Text


    Ans = (RS * RS) * HT
    Sum = 0.33 * 3.14
    Volume = Sum * Ans
    lblResult.Caption = "The Volume is " & Volume
    End If


    Sum = 0.33 * 3.14
    Ans = (RS * RS) * HT
    Volume = Sum * Ans
    lblResult.Caption = "The Volume is " & Volume


    End Sub

    Private Sub Form_Load()
    txtHeight.Text = 0
    txtRadius.Text = 0
    End Sub

    Any help would be much apreciated


Comments

  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    RS = txtHeight.Text / 2

    omg

    Don't interchange between strings and numbers. Just because VB lets you doesn't mean it's good coding.


    on another note.
    1. Have you tried stepping through your code?
    2. Debug error means nothing. What exactly is the error and where does it occur?
    3. Remark your code more.

    Also I don't understand this...
    If txtHeight.Text > 0 Then
    txtRadius.Visible = False

    this would make the field invisible but your still using that field in your code?


  • Registered Users Posts: 2,781 ✭✭✭amen


    Ivan
    1
    2H= 2R
    2R is NOT the same as R * R
    ie R=8 then 2R ie 2(8) = 16
    R *R ie 8 *8 = 64

    2: as Hobbers mentioned
    RS = txtHeight.Text / 2 is bad
    you should first off all check to make sure that txtHeight is an actual number. Have a look at IsNumeric()

    3: Integer limitiation is 32256 (ish) so if height is 100 then you are going to get overflow errors. Or is 10.25 is entered you will also get an overflow. Have a look at Double data type

    4: indetation and comments. Indent your code

    5: you code does work if you use small integer values. Have a look at point 3

    6: a nice function to perform the calulation would be nice

    7: what is
    Sum = 0.33 * 3.14 doing why not jsut set Sum up as a constant and = 0.33*3.14
    Call a constant Sum is a bit silly sounds like a key word


Advertisement