# vb fractions calculator

Page 1 of 1

## 4 Replies - 13239 Views - Last Post: 25 October 2010 - 06:23 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=196460&amp;s=6feeaf26975dff8aaf1e67a589adb237&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 leftymcmatty

Reputation: 0
• Posts: 2
• Joined: 23-October 10

# vb fractions calculator

Posted 23 October 2010 - 08:38 PM

Alright this is the deal I am writing a fractions calculator and I am obviously not passing arguments between procedures correctly because I keep getting a 0 as an answer in the lblSolution label.
Here is the code:
``` Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim strDenominatorOne As String
Dim strNumeratorOne As String
Dim strDenominatorTwo As String
Dim strNumeratorTwo As String
Dim decDenominatorOne As Decimal
Dim decNumeratorOne As Decimal
Dim decDenominatorTwo As Decimal
Dim decNumeratorTwo As Decimal
Dim decSolution As Decimal

strDenominatorOne = CStr(Me.txtDenominatorOne.Text)
If IsNumeric(strDenominatorOne) Then
decDenominatorOne = Convert.ToDecimal(strDenominatorOne)
Else
MsgBox("Please enter only numbers for Denominator one")
End If
strNumeratorOne = CStr(Me.txtNumeratorOne.Text)
If IsNumeric(strNumeratorOne) Then
decNumeratorOne = Convert.ToDecimal(strNumeratorOne)
Else
MsgBox("Please enter only numbers for Numerator One")
End If
strDenominatorTwo = CStr(Me.txtDenominatorTwo.Text)
If IsNumeric(strDenominatorTwo) Then
decDenominatorTwo = Convert.ToDecimal(strDenominatorTwo)
Else
MsgBox("Please enter only numbers for Denominator Two")
End If
strNumeratorTwo = CStr(Me.txtNumeratorTwo.Text)
If IsNumeric(strNumeratorTwo) Then
decNumeratorTwo = Convert.ToDecimal(strNumeratorTwo)
Else
MsgBox("Please enter only numbers for Numerator Two")
End If

FractionConversionOne(decDenominatorOne, decNumeratorOne)
FractionConversionTwo(decDenominatorTwo, decNumeratorTwo)

decSolution = Division(decSolution)
lblSolution.Text = decSolution.ToString
lblSolution.Text = decSolution.ToString
decSolution = Subtraction(decSolution)
lblSolution.Text = decSolution.ToString
Else
decSolution = Multiplication(decSolution)
lblSolution.Text = decSolution.ToString

End If

End Sub
Private Sub FractionConversionOne(ByVal decDenominatorOne As Decimal, ByVal decNumeratorOne As Decimal)
'This procedure converts the first entered fraction into a decimal
Dim decSolutionOne As Decimal

Try
decSolutionOne = decDenominatorOne / decNumeratorOne
Catch Exception As DivideByZeroException
MsgBox("You can not use Zero as a denominator")
Catch Exception As ArgumentNullException
MsgBox("Please enter a number in each box")
Catch Exception As FormatException
MsgBox("Please enter only numbers in the fields")
End Try
End Sub
Private Sub FractionConversionTwo(ByVal decDenominatorTwo As Decimal, ByVal decNumeratorTwo As Decimal)
'This procedure converts the second entered fraction into a decimal
Dim decSolutionTwo As Decimal

Try
decSolutionTwo = decDenominatorTwo / decNumeratorTwo
Catch Exception As DivideByZeroException
MsgBox("You can not use Zero as a denominator")
Catch Exception As ArgumentNullException
MsgBox("Please enter a number in each box")
Catch Exception As FormatException
MsgBox("Please enter only numbers in the fields")
End Try
End Sub
Private Function Addition(ByVal decSum As Decimal) As Decimal
Dim decOne As Decimal
Dim decTwo As Decimal

decSum = decOne + decTwo

Return decSum
End Function
Private Function Subtraction(ByVal decDifference As Decimal) As Decimal
Dim decOne As Decimal
Dim decTwo As Decimal

decDifference = decOne - decTwo

Return decDifference
End Function
Private Function Multiplication(ByVal decProduct As Decimal) As Decimal
Dim decOne As Decimal
Dim decTwo As Decimal
decProduct = decOne * decTwo

Return decProduct
End Function
Private Function Division(ByVal decQuotient As Decimal) As Decimal
Dim decOne As Decimal
Dim decTwo As Decimal
Try
decQuotient = decOne / decTwo
Catch Exception As DivideByZeroException
MsgBox("Attempt to Divide by Zero")
End Try
Return decQuotient
End Function
```

HELPPP!!!!!
HELP!!!!!!!!!!

This post has been edited by AdamSpeight2008: 23 October 2010 - 08:45 PM
Reason for edit:: Please remember to use the code tags for code.

Is This A Good Question/Topic? 0

## Replies To: vb fractions calculator

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

## Re: vb fractions calculator

Posted 23 October 2010 - 08:46 PM

Your is not vb6 but vb.net, so moving to correct section.

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

## Re: vb fractions calculator

Posted 23 October 2010 - 09:18 PM

You could do with a review of the basics of vb.net.
See my beginner's blog: VB Peasy Lemon Squeezy.

### #4 leftymcmatty

Reputation: 0
• Posts: 2
• Joined: 23-October 10

## Re: vb fractions calculator

Posted 25 October 2010 - 03:28 PM

YES!!!!!!!! I figured it out and my program is working. But I still have a query. How do I limit the result to the hundredths place?
here is the fixed code ( He can be taught!!)
```Public Class frmFractionsCalculator

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

Dim decFractionValueOne As Decimal
Dim decFractionValueTwo As Decimal
Dim decSolution As Decimal

FractionConversionOne(decFractionValueOne) 'decSolutionOne in FractionConversionTwo Private Function
FractionConversionTwo(decFractionValueTwo) 'decSolutionTwo in FractionConversionTwo Private Function

decSolution = Division(decFractionValueOne, decFractionValueTwo)
lblSolution.Text = decSolution.ToString
lblSolution.Text = decSolution.ToString
decSolution = Subtraction(decFractionValueOne, decFractionValueTwo)
lblSolution.Text = decSolution.ToString
Else
decSolution = Multiplication(decFractionValueOne, decFractionValueTwo)
lblSolution.Text = decSolution.ToString

End If
End Sub
Private Function FractionConversionOne(ByRef decSolutionOne As Decimal)
'This procedure converts the first entered fraction into a decimal
Dim strDenominatorOne As String
Dim strNumeratorOne As String
Dim decDenominatorOne As Decimal
Dim decNumeratorOne As Decimal

strDenominatorOne = CStr(Me.txtDenominatorOne.Text)
If IsNumeric(strDenominatorOne) Then
decDenominatorOne = Convert.ToDecimal(strDenominatorOne)
Else
MsgBox("Please enter only numbers for Denominator one")
End If
strNumeratorOne = CStr(Me.txtNumeratorOne.Text)
If IsNumeric(strNumeratorOne) Then
decNumeratorOne = Convert.ToDecimal(strNumeratorOne)
Else
MsgBox("Please enter only numbers for Numerator One")
End If

Try
decSolutionOne = decNumeratorOne / decDenominatorOne
Catch Exception As DivideByZeroException
MsgBox("You can not use Zero as a denominator")
Catch Exception As ArgumentNullException
MsgBox("Please enter a number in each box")
Catch Exception As FormatException
MsgBox("Please enter only numbers in the fields")
End Try
Return decSolutionOne

End Function
Private Function FractionConversionTwo(ByRef decSolutionTwo As Decimal)
'This procedure converts the second entered fraction into a decimal
Dim strDenominatorTwo As String
Dim strNumeratorTwo As String
Dim decDenominatorTwo As Decimal
Dim decNumeratorTwo As Decimal
strDenominatorTwo = CStr(Me.txtDenominatorTwo.Text)
If IsNumeric(strDenominatorTwo) Then
decDenominatorTwo = Convert.ToDecimal(strDenominatorTwo)
Else
MsgBox("Please enter only numbers for Denominator Two")
End If
strNumeratorTwo = CStr(Me.txtNumeratorTwo.Text)
If IsNumeric(strNumeratorTwo) Then
decNumeratorTwo = Convert.ToDecimal(strNumeratorTwo)
Else
MsgBox("Please enter only numbers for Numerator Two")
End If

Try
decSolutionTwo = decNumeratorTwo / decDenominatorTwo
Catch Exception As DivideByZeroException
MsgBox("You can not use Zero as a denominator")
Catch Exception As ArgumentNullException
MsgBox("Please enter a number in each box")
Catch Exception As FormatException
MsgBox("Please enter only numbers in the fields")
End Try
Return decSolutionTwo
End Function
Private Function Addition(ByRef decOne As Decimal, ByRef decTwo As Decimal) As Decimal
Dim decSum As Decimal

decSum = decOne + decTwo

Return decSum
End Function
Private Function Subtraction(ByRef decOne As Decimal, ByRef decTwo As Decimal) As Decimal
Dim decdifference As Decimal

decDifference = decOne - decTwo

Return decDifference
End Function
Private Function Multiplication(ByRef decOne As Decimal, ByRef decTwo As Decimal) As Decimal
Dim decProduct As Decimal

decProduct = decOne * decTwo

Return decProduct
End Function
Private Function Division(ByRef decOne As Decimal, ByRef decTwo As Decimal) As Decimal
Dim decQuotient As Decimal

Try
decQuotient = decOne / decTwo
Catch Exception As DivideByZeroException
MsgBox("Attempt to Divide by Zero")
End Try
Return decQuotient
End Function

Private Sub frmFractionsCalculator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

End Class
```

### #5 _HAWK_

• Master(Of Foo)

Reputation: 1162
• Posts: 4,444
• Joined: 02-July 08

## Re: vb fractions calculator

Posted 25 October 2010 - 06:23 PM

Not sure why you are converting a string to a string and then converting to a Double later - just convert it the first time and move on. Your ByRef parameters should be ByVal.
```strDenominatorTwo = CStr(Me.txtDenominatorTwo.Text)
If IsNumeric(strDenominatorTwo) Then
decDenominatorTwo = Convert.ToDecimal(strDenominatorTwo)
...
```

So like this:

```decDenominatorTwo = Double.Parse(Me.txtDenominatorTwo.Text)
'or
Dim result As Double
decDenominatorTwo = Double.TryParse(Me.txtDenominatorTwo.Text, result)

```

This post has been edited by hawkvalley1: 25 October 2010 - 06:24 PM