RBCC Posted February 25, 2004 Posted February 25, 2004 Dim WithEvents clsfraction As New fraction Dim WithEvents clsfraction1 As New fraction Dim denominator, denominator1 As Int16 Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click Dim common, numer1, numer2, denom, total As Int16 Dim gcd As Int16 'holds value of gcd numer1 = Val(txtnum1.Text) numer2 = Val(txtnum2.Text) denominator = Val(txtdenom1.Text) denominator1 = Val(txtdenom2.Text) common = clsfraction.calculate_common_denominator(denominator, denominator1) total = clsfraction.add(clsfraction, clsfraction1) gcd = clsfraction.reduce(total, common) common = common / gcd total = total / gcd txtsumnum.Text = total txtsumdenom.Text = common End Sub Private Sub clsfraction1_zerodenom() Handles clsfraction1.zerodenom MessageBox.Show("Error-Zero in the denominator") End Sub End Class ;***************Classes****************************** Public Class fraction Dim m_numerator As Int16 Dim m_denominator As Int16 Public Event zerodenom() Public Property numerator() As Int16 Get Return m_numerator End Get Set(ByVal Value As Int16) Value = m_numerator End Set End Property Public Property denominator() As Int16 Get Return m_denominator End Get Set(ByVal Value As Int16) Value = m_denominator End Set End Property Public Function reduce(ByVal numerator As Int16, ByVal denominator As Int16) As Int16 Dim temp As Int16 Do While denominator <> 0 temp = denominator denominator = numerator Mod denominator numerator = temp Loop Return numerator End Function Public Function calculate_common_denominator(ByVal denominator As Int16, ByVal denominator1 As Int16) Dim common As Int16 common = denominator * denominator1 Return common End Function Public Function add(ByVal frac1 As fraction, ByVal frac2 As fraction) Dim sum1, sum2, total As Int16 'How do I pass the numerator and denominator of each fraction?? total = sum1 + sum2 End Function End Class :confused: John Quote
Administrators PlausiblyDamp Posted February 25, 2004 Administrators Posted February 25, 2004 (edited) Not sure what you are asking here - what do you want the add function to do? Also what data type should it return? If you want a new fraction with the number and denominator totalled then the following should work Public Function add(ByVal frac1 As fraction, ByVal frac2 As fraction) 'How do I pass the numerator and denominator of each fraction?? Dim tmp As New fraction() tmp.numerator = frac1.numerator + frac2.numerator tmp.denominator = frac1.denominator + frac2.denominator Return tmp End Function Edited February 19, 2007 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.