locked
I need a fresh set of eyes. RRS feed

  • Question

  • Hello! I need help identifying the error within the code for decPounds. It just always ends up coming out to be 0.0 everytime I calculate it.  Any help would be greatly appreciated. Thank You!

            Dim strWeightInput As String
            Dim decMass As Decimal
            Dim decPounds As Decimal
            Dim decKilograms As Decimal

            strWeightInput = txtWeightInput.Text
            If IsNumeric(strWeightInput) Then
                decMass = Convert.ToDecimal(strWeightInput)
                If decMass > 0 Then
                    'The input has passed the error checks! The program will proced.
                    If radConvertPounds.Checked Then
                        decPounds = decMass * 2.2
                    Else
                        If radConvertKilograms.Checked Then
                            decKilograms = decMass / 2.2
                        End If
                    End If

                    'For error that may come up with the new decimals.
                Else
                    'This check is for any Numbers lower than 0.
                    MsgBox("Mass should greater than 0. Please ReEnter", 16, "Input Error")
                End If

            Else
                'This check is for any NonNumeric character.
                MsgBox("Mass should be Numeric. Please ReEnter", 16, "Input Error")
            End If
            lblConverted.Text = decPounds.ToString("F1") & "Converted to Kilograms"
            lblConverted.Text = decKilograms.ToString("F1") & " Converted to Pounds"
       

       
    Wednesday, March 5, 2014 2:10 AM

Answers

  • Oh ok. It is meant for a user to leave the mass in a textbox and then it will take that number and translate it into pounds if you selected kilograms. And kilograms if you selected pounds. Basically a simple weight converter.   

    Ah ok.

    Consider using the .Validation event.

    A while back I wrote this up which has what I think is a fairly good explanation of using that. You might also want to put some of that into a function, maybe like this:

    Option Strict On Option Explicit On Public Class Form1 Private Enum Units Pounds Kilograms End Enum Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load ' Test it here End Sub Private Function ConvertMass(ByVal mass As Decimal, _ ByVal unitsToConvertTo As Units) As Decimal Dim retval As Decimal = 0 Try Select Case unitsToConvertTo Case Units.Kilograms retval = CDec(mass / 2.2) Case Units.Pounds retval = CDec(mass * 2.2) End Select Catch ex As Exception MessageBox.Show("An error occurred:" & vbCrLf & vbCrLf & ex.Message, _ "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Warning) End Try Return retval End Function End Class

    I hope that gives you some ideas. :)


    Please call me Frank :)

    Wednesday, March 5, 2014 2:49 AM

All replies

  • Koala,

    I would do all of that a good bit differently, but what method is that in?


    Please call me Frank :)

    Wednesday, March 5, 2014 2:20 AM
  • Hello Frank. 

    It is a Windows Form Application. Is that what you were looking for?

    Wednesday, March 5, 2014 2:24 AM
  • Hello Frank. 

    It is a Windows Form Application. Is that what you were looking for?

    Well, no - I assumed that much, but thanks for the confirmation. :)

    I meant -- when is that called? For example is that after the user leaves a TextBox or ... ?


    Please call me Frank :)


    Wednesday, March 5, 2014 2:29 AM
  • Oh ok. It is meant for a user to leave the mass in a textbox and then it will take that number and translate it into pounds if you selected kilograms. And kilograms if you selected pounds. Basically a simple weight converter.   
    Wednesday, March 5, 2014 2:43 AM
  • Oh ok. It is meant for a user to leave the mass in a textbox and then it will take that number and translate it into pounds if you selected kilograms. And kilograms if you selected pounds. Basically a simple weight converter.   

    Ah ok.

    Consider using the .Validation event.

    A while back I wrote this up which has what I think is a fairly good explanation of using that. You might also want to put some of that into a function, maybe like this:

    Option Strict On Option Explicit On Public Class Form1 Private Enum Units Pounds Kilograms End Enum Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load ' Test it here End Sub Private Function ConvertMass(ByVal mass As Decimal, _ ByVal unitsToConvertTo As Units) As Decimal Dim retval As Decimal = 0 Try Select Case unitsToConvertTo Case Units.Kilograms retval = CDec(mass / 2.2) Case Units.Pounds retval = CDec(mass * 2.2) End Select Catch ex As Exception MessageBox.Show("An error occurred:" & vbCrLf & vbCrLf & ex.Message, _ "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Warning) End Try Return retval End Function End Class

    I hope that gives you some ideas. :)


    Please call me Frank :)

    Wednesday, March 5, 2014 2:49 AM
  • Thank You Frank! I will try this in the morning!
    Wednesday, March 5, 2014 2:51 AM
  • Thank You Frank! I will try this in the morning!

    You're welcome. :)

    Fatigue has a way of making all of us "not think straight". ;-)


    Please call me Frank :)

    Wednesday, March 5, 2014 2:53 AM