CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Guest

    simplify problem

    i made a VB code that would find the LCM between two numbers. Here it is



    public Function Math_LCM(n1 as Double, n2 as Double) as Double
    LCM = n1 * n2
    n0 = n1 * n2
    x = 2
    Do: DoEvents
    n1 = (n1 / (x - 1)) * x
    If Math_IsInteger(n1 / n2) then LCM = n1: Exit Do
    x = x + 1
    Loop Until n1 = n0
    Math_LCM = LCM
    End Function





    It worked when I used it alone. I did 3, 5 - it gave back 15. 2, 4 - it gave back 4. Anyway, then I tried to make VB code that would simplify a fraction. Here is what I got:



    public Function Math_Simplify(numerator as Double, denominator as Double) as string
    If Not Math_IsInteger(numerator) then Exit Function
    If Not Math_IsInteger(denominator) then Exit Function
    n = Val(numerator)
    d = denominator
    n0 = Math_LCM(n, d)
    x = n0 / n
    n = n * x
    d = d * x
    Math_Simplify = Trim(Str(n)) + "/" + Trim(Str(d))
    End Function





    Before I go on, Math_IsInteger(x) checks if x is an integer by using the Fix() function. Anyway, I keep getting the error 'ByRef Argument Type Mismatch' - and VB5 highlights the "n" in the line of code:

    n0 = Math_LCM(n, d)



    I don't understand it...Can you help me?




  2. #2
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: simplify problem

    By not specifying ByVal in your Functions VB assumes you're passing variables by Reference, which require the Parameters to be an exact Type Match and in your function Math_Simplify you use variables that you haven't dimensioned, so they default to Empty which causes the Type Mismatch Error.

    To rectify this either change your functions to pass ByVal or Dimension the variables; n, d as Double in your Function, ie.
    public Function Math_Simplify(numerator as Double, denominator as Double) as string
    Dim n as Double
    Dim d as Double
    If Not Math_IsInteger(numerator) then Exit Function
    If Not Math_IsInteger(denominator) then Exit Function
    n = Val(numerator)
    d = denominator
    n0 = Math_LCM(n, d)
    x = n0 / n
    n = n * x
    d = d * x
    Math_Simplify = Trim(Str(n)) + "/" + Trim(Str(d))
    End Function




    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

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

Featured