I'm currently doing an assignment which calculates the surface area of a cylinder given the input of radius and height from the user and the answer is displayed in a read only control this is what I have so far
Class code
Public Class SurfaceAreaCalculator
Public Function calcSurfaceArea(ByVal r As Double, ByVal h As Double)
Const Pi As Double = 3.14159265
' r = radius, h = height
Return 2 * Pi * r ^ 2 + 2 * Pi * r * h
End Function
End Class
Form Code
Public Class WebForm1
Inherits System.Web.UI.Page
Dim SurfaceArea As New SurfaceAreaCalculator
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If IsPostBack Then
SurfaceArea = Session("SurfaceAreaClass")
Else
SurfaceArea = New SurfaceAreaCalculator
Session("SurfaceAreaClass") = SurfaceArea
End If
End Sub
Private Sub butcalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butcalculate.Click
Me.txtanswer.Text = SurfaceArea.calcSurfaceArea
I keep getting an error in the button click event procedure,and am having trouble displaying the answer using the formula above
Any tips would be appreciated ThanksPublic Class SurfaceAreaCalculator
Public Function calcSurfaceArea(ByVal r As Double, ByVal h As Double)
Const Pi As Double = 3.14159265
' r = radius, h = height
Return 2 * (Pi * r ^ 2) + (2 * Pi * r) * h
End Function
End Class
Public Class WebForm1
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If not IsPostBack Then
Session("SurfaceAreaClass") = New SurfaceAreaCalculator
End If
End Sub
Private Sub butcalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butcalculate.Click
Dim SurfaceArea as SerfaceAreaCalculator = Session("SurfaceAreaClass")
Me.txtanswer.Text = SurfaceArea.calcSurfaceArea(Put something in here)
Thanks for that you've been a great help, would it be possible to help with the following as well
The program also requires that the user enters a double precision number between 0 and 5000 for the radius textbox and a double precision number between 0 and 10000 for the height textbox, the answer displayed in the third text box also has to be to 2 decimal places
I have used a required field validator control to ensure the textboxes can't be blank, but unsure on how to make sure the values are both double precision and in between those ranges I probably also would need to ensure the user can't enter non-numeric data
Any tips/advice on the above would be appreciated as I'm new to the language and still learning
Thank-you
Use a regex validator with your required field validator. Here's an example
<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server"
ErrorMessage="Please enter a double precision number between 0.00 and 5000.00"
ValidationExpression="(^5000\.00$)|((^[0-4]\d{3}|^\d{0,3})\.\d\d$)" ControlToValidate="TextBox1">
</asp:RegularExpressionValidator>
The validationexpression value breaks down like this
(^5000\.00$) - Match 5000.00
| or
^[0-4]\d{3} - Match any number starting with 0 through 4 ending in any three digits. This will match 0000-4999
^\d{0,3} - Match any number starting with 0 through three digits long. 0 - 999
\.\d\d$ - Match numbers ending with a decimal and any two numbers.
Thanks for your help
Thursday, March 22, 2012
Surface Area Calculator
Labels:
area,
asp,
assignment,
calculates,
calculator,
cylinder,
height,
input,
net,
radius,
surface,
user
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment