Results 1 to 2 of 2

Thread: Why doesn't this fractions function work?

  1. #1
    Guest

    Hi! I have two picture boxes and two command buttons. Command1 gives two random numbers in Picture1, fractions, like:

    7/2 or 12/4 or 29/14

    Then, when command2 is clicked, the fraction is simplified in Picture2.

    7/2 would become 3 1/2, 12/4 becomes 3, 29/14 becomes 2 1/29
    etc....

    My code works fine when I have it in command1 and command2 like this:

    Dim numer As Integer
    Dim denom As Integer

    Private Sub Command1_Click()
    Randomize
    numer = Int(Rnd * 30) + 1
    denom = Int(Rnd * 10) + 1
    Picture1.Cls
    Picture1.Print numer & "/" & denom

    End Sub

    Private Sub Command2_Click()
    Picture2.Cls
    Dim lowestfraction As String
    If numer Mod denom <> 0 Then
    lowestfraction = IIf(Int(numer / denom) > 0, Int(numer / denom), "") & " " & numer Mod denom & "/" & denom
    End If
    If numer Mod denom = 0 Then
    lowestfraction = numer / denom
    End If
    Picture2.Print lowestfraction
    End Sub


    But I want the code in command2 to be a function that I can call throughout my project. All it returns is '0'.

    Anybody got any ideas, please? This is the way I have my code:


    Private Sub Command1_Click()
    Randomize
    numer = Int(Rnd * 30) + 1
    denom = Int(Rnd * 10) + 1
    Picture1.Cls
    Picture1.Print numer & "/" & denom
    End Sub

    Private Sub Command2_Click()
    Picture2.Cls
    Picture2.Print ReduceFractions(numer, denom)
    End Sub

    Public Function ReduceFractions(ByVal upper As Integer, ByVal lower As Integer) As Integer
    Dim lowestfraction As String
    If upper Mod lower <> 0 Then
    lowestfraction = IIf(Int(upper / lower) > 0, Int(upper / lower), "") & " " & upper Mod lower & "/" & lower
    End If
    If upper Mod lower = 0 Then
    lowestfraction = upper / lower
    End If
    End Function

  2. #2
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    First, make the ReduceFractions function return a String instead of an Integer.
    Code:
    Public Function ReduceFractions(ByVal upper As Integer, ByVal lower As Integer) As String
        ' ReduceFractions code goes here
    Second, make the ReduceFractions function return the lowestfraction variable.
    Code:
        ReduceFractions = lowestfraction
    End Function
    Alrighty? Enjoy!

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