1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Visual Basic Help

Discussion in 'Software Development' started by Rhino24, Mar 3, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. Rhino24

    Rhino24 Thread Starter

    Joined:
    Feb 8, 2008
    Messages:
    41
    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
    Else
    Factorial = x * Factorial(x - 1)
    End If

    End Function
     
  2. calvin-c

    calvin-c Banned

    Joined:
    May 17, 2006
    Messages:
    1,098
    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.
     
  3. Rhino24

    Rhino24 Thread Starter

    Joined:
    Feb 8, 2008
    Messages:
    41
    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
     
  4. calvin-c

    calvin-c Banned

    Joined:
    May 17, 2006
    Messages:
    1,098
    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.
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/689525

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice