vb fractions calculator

problem passing arguments between procedures

Page 1 of 1

4 Replies - 6518 Views - Last Post: 25 October 2010 - 06:23 PM Rate Topic: -----

#1 leftymcmatty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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)

        If radDivide.Checked = True Then
            decSolution = Division(decSolution)
            lblSolution.Text = decSolution.ToString
        ElseIf radAdd.Checked = True Then
            decSolution = Addition(decSolution)
            lblSolution.Text = decSolution.ToString
        ElseIf radSubtract.Checked = True Then
            decSolution = Subtraction(decSolution)
            lblSolution.Text = decSolution.ToString
        Else
            radMultiply.Checked = True
            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

#2 AdamSpeight2008  Icon User is online

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is online

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 leftymcmatty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

        If radDivide.Checked = True Then
            decSolution = Division(decFractionValueOne, decFractionValueTwo)
            lblSolution.Text = decSolution.ToString
        ElseIf radAdd.Checked = True Then
            decSolution = Addition(decFractionValueOne, decFractionValueTwo)
            lblSolution.Text = decSolution.ToString
        ElseIf radSubtract.Checked = True Then
            decSolution = Subtraction(decFractionValueOne, decFractionValueTwo)
            lblSolution.Text = decSolution.ToString
        Else
            radMultiply.Checked = True
            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

Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,097
  • 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

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1