Click to See Complete Forum and Search --> : simplify problem


January 29th, 2000, 03:30 PM
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?

Aaron Young
January 29th, 2000, 11:15 PM
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
ajyoung@pressenter.com
aarony@redwingsoftware.com