Tech Support Guy banner

Visual Basic Help

1267 Views 3 Replies 2 Participants Last post by  calvin-c
Can anyone help me correct this code?? I have to figure out what it does and design the GUI in the form.

Private Sub btnFactorial_Click( _
ByVal eventSender As System.Object, _
ByVal eventArgs As System.EventArgs) _
Handles btnFactorial.Click
Dim x As Double
Dim Fact As Double

lblOutput.Text = "To determine the Factorial of a number. " _
& "Please enter a number less than 10."
& "Then, click on the Factorial Button to view the results."

x = Val(txtInput.Text)

Fact = Factorial(x)
lblOutput.Text = ("")
lblOutput.Text = (" " & x & "! is " & Fact)

End Sub

Private Function Factorial(ByRef x As Double) As Double

If x <= 1 Then
Factorial = 1
Factorial = x * Factorial(x - 1)
End If

End Function
See less See more
Not open for further replies.
1 - 4 of 4 Posts
Well, you'd have a label, a textbox, and a button. The user would enter a number in the textbox & click the button-then this would calculate the factorial & display it in the label. BTW, the lblOutput instructions don't belong here. They'd not only not show up until the user clicked on the button (and how would they know to do that if the instructions aren't shown?) but they'd also be overwritten by the output.
Yeah, I kinda understood that part, but my main question was I am getting a few errors in the code and wanted to know if anyone could spot them right away
What kind of errors? I don't see anything obvious. One change I'd make is an explicit Return on the function, especially since it's being called recursively-but it should work without it. txtInput must contain a number, of course, before you click on btnFactorial-there's no error checking for that but, if it doesn't then (as I recall) Val returns 0 & the routine should still work, displaying 0! is 1.
1 - 4 of 4 Posts
Not open for further replies.