Results 1 to 4 of 4

Thread: FUNCTION: Length Conversion, is there an easier way?

  1. #1

    Thread Starter
    Lively Member CHEEZEnCRACKERS's Avatar
    Join Date
    Feb 2012
    Location
    Kentucky
    Posts
    67

    Question FUNCTION: Length Conversion, is there an easier way?

    This works perfectly fine but was incredibly long to write. Just wondering if there is a better way to write this. I need to be able to convert between mm, cm, in, ft, yrd, and meter and anyway in-between. I'm mainly just using this to convert numbers in the background not really part on the interface portion of the form. If anyone has any suggestions please let me know. Thanks

    Code:
        Private Sub nudCONxEnterNum_ValueChanged(sender As Object, e As EventArgs) Handles nudCONxEnterNum.ValueChanged, cbCONxLenthSelection.SelectedIndexChanged
    
            Dim enterNum As Decimal
    
            Dim MMtoCM As Decimal
            Dim MMtoIN As Decimal
            Dim MMtoFT As Decimal
            Dim MMtoYRD As Decimal
            Dim MMtoM As Decimal
    
            Dim CMtoMM As Decimal
            Dim CMtoIN As Decimal
            Dim CMtoFT As Decimal
            Dim CMtoYRD As Decimal
            Dim CMtoM As Decimal
    
            Dim INtoMM As Decimal
            Dim INtoCM As Decimal
            Dim INtoFT As Decimal
            Dim INtoYRD As Decimal
            Dim INtoM As Decimal
    
            Dim FTtoMM As Decimal
            Dim FTtoCM As Decimal
            Dim FTtoIN As Decimal
            Dim FTtoYRD As Decimal
            Dim FTtoM As Decimal
    
            Dim YRDtoMM As Decimal
            Dim YRDtoCM As Decimal
            Dim YRDtoIN As Decimal
            Dim YRDtoFT As Decimal
            Dim YRDtoM As Decimal
    
            Dim MtoMM As Decimal
            Dim MtoCM As Decimal
            Dim MtoIN As Decimal
            Dim MtoFT As Decimal
            Dim MtoYRD As Decimal
    
            Decimal.TryParse(nudCONxEnterNum.Text, enterNum)
    
            If cbCONxLenthSelection.SelectedIndex = 0 Then
                MMtoCM = CDec(millimeters_TO_centimeters(enterNum))
                MMtoIN = CDec(millimeters_TO_inches(enterNum))
                MMtoFT = CDec(millimeters_TO_feet(enterNum))
                MMtoYRD = CDec(millimeters_TO_yards(enterNum))
                MMtoM = CDec(millimeters_TO_meters(enterNum))
                tbCONxMM.Text = nudCONxEnterNum.Value.ToString("N4") & " mm"
                tbCONxCM.Text = MMtoCM.ToString("N4") & " cm"
                tbCONxIN.Text = MMtoIN.ToString("N4") & " in"
                tbCONxFT.Text = MMtoFT.ToString("N4") & " ft"
                tbCONxYRD.Text = MMtoYRD.ToString("N4") & " Yrd"
                tbCONxM.Text = MMtoM.ToString("N4") & " M"
            ElseIf cbCONxLenthSelection.SelectedIndex = 1 Then
                CMtoMM = CDec(centimeters_TO_millimeters(enterNum))
                CMtoIN = CDec(centimeters_TO_inches(enterNum))
                CMtoFT = CDec(centimeters_TO_feet(enterNum))
                CMtoYRD = CDec(centimeters_TO_yards(enterNum))
                CMtoM = CDec(centimeters_TO_meters(enterNum))
                tbCONxMM.Text = CMtoMM.ToString("N4") & " mm"
                tbCONxCM.Text = nudCONxEnterNum.Value.ToString("N4") & " cm"
                tbCONxIN.Text = CMtoIN.ToString("N4") & " in"
                tbCONxFT.Text = CMtoFT.ToString("N4") & " ft"
                tbCONxYRD.Text = CMtoYRD.ToString("N4") & " Yrd"
                tbCONxM.Text = CMtoM.ToString("N4") & " M"
            ElseIf cbCONxLenthSelection.SelectedIndex = 2 Then
                INtoMM = CDec(inches_TO_millimeters(enterNum))
                INtoCM = CDec(inches_TO_centimeters(enterNum))
                INtoFT = CDec(inches_TO_feet(enterNum))
                INtoYRD = CDec(inches_TO_yards(enterNum))
                INtoM = CDec(inches_TO_meters(enterNum))
                tbCONxMM.Text = INtoMM.ToString("N4") & " mm"
                tbCONxCM.Text = INtoCM.ToString("N4") & " cm"
                tbCONxIN.Text = nudCONxEnterNum.Value.ToString("N4") & " in"
                tbCONxFT.Text = INtoFT.ToString("N4") & " ft"
                tbCONxYRD.Text = INtoYRD.ToString("N4") & " Yrd"
                tbCONxM.Text = INtoM.ToString("N4") & " M"
            ElseIf cbCONxLenthSelection.SelectedIndex = 3 Then
                FTtoMM = CDec(feet_TO_millimeters(enterNum))
                FTtoCM = CDec(feet_TO_centimeters(enterNum))
                FTtoIN = CDec(feet_TO_inches(enterNum))
                FTtoYRD = CDec(feet_TO_yards(enterNum))
                FTtoM = CDec(feet_TO_meters(enterNum))
                tbCONxMM.Text = FTtoMM.ToString("N4") & " mm"
                tbCONxCM.Text = FTtoCM.ToString("N4") & " cm"
                tbCONxIN.Text = FTtoIN.ToString("N4") & " in"
                tbCONxFT.Text = nudCONxEnterNum.Value.ToString("N4") & " ft"
                tbCONxYRD.Text = FTtoYRD.ToString("N4") & " Yrd"
                tbCONxM.Text = FTtoM.ToString("N4") & " M"
            ElseIf cbCONxLenthSelection.SelectedIndex = 4 Then
                YRDtoMM = CDec(yards_TO_millimeters(enterNum))
                YRDtoCM = CDec(yards_TO_centimeters(enterNum))
                YRDtoIN = CDec(yards_TO_inches(enterNum))
                YRDtoFT = CDec(yards_TO_feet(enterNum))
                YRDtoM = CDec(yards_TO_meters(enterNum))
                tbCONxMM.Text = YRDtoMM.ToString("N4") & " mm"
                tbCONxCM.Text = YRDtoCM.ToString("N4") & " cm"
                tbCONxIN.Text = YRDtoIN.ToString("N4") & " in"
                tbCONxFT.Text = YRDtoFT.ToString("N4") & " ft"
                tbCONxYRD.Text = nudCONxEnterNum.Value.ToString("N4") & " Yrd"
                tbCONxM.Text = YRDtoM.ToString("N4") & " M"
            ElseIf cbCONxLenthSelection.SelectedIndex = 5 Then
                MtoMM = CDec(meters_TO_millimeters(enterNum))
                MtoCM = CDec(meters_TO_centimeters(enterNum))
                MtoIN = CDec(meters_TO_inches(enterNum))
                MtoFT = CDec(meters_TO_feet(enterNum))
                MtoYRD = CDec(meters_TO_yards(enterNum))
                tbCONxMM.Text = MtoMM.ToString("N4") & " mm"
                tbCONxCM.Text = MtoCM.ToString("N4") & " cm"
                tbCONxIN.Text = MtoIN.ToString("N4") & " in"
                tbCONxFT.Text = MtoFT.ToString("N4") & " ft"
                tbCONxYRD.Text = MtoYRD.ToString("N4") & " Yrd"
                tbCONxM.Text = nudCONxEnterNum.Value.ToString("N4") & " M"
            End If
    
        End Sub
        Private Sub btnCONxReset_Click(sender As Object, e As EventArgs) Handles btnCONxReset.Click
            ''''NOTE: "Conversions" - Reset button
            nudCONxEnterNum.Text = "1.0000"
            'cbCONxLenthSelection.SelectedIndex = 0
            nudCONxEnterNum.Focus()
        End Sub
        ''''NOTE: Conversion "MILLIMETER to XX"_______________________________________
        Public Function millimeters_TO_centimeters(ByVal answer As Decimal) As String
            Dim millimeters As Decimal = nudCONxEnterNum.Value
            answer = millimeters * 0.1D
            millimeters_TO_centimeters = answer.ToString
        End Function
        Public Function millimeters_TO_inches(ByVal answer As Decimal) As String
            Dim millimeters As Decimal = nudCONxEnterNum.Value
            answer = millimeters * 0.03937007874D
            millimeters_TO_inches = answer.ToString
        End Function
        Public Function millimeters_TO_feet(ByVal answer As Decimal) As String
            Dim millimeters As Decimal = nudCONxEnterNum.Value
            answer = millimeters * 0.003280839895D
            millimeters_TO_feet = answer.ToString
        End Function
        Public Function millimeters_TO_yards(ByVal answer As Decimal) As String
            Dim millimeters As Decimal = nudCONxEnterNum.Value
            answer = millimeters * 0.0010936132983D
            millimeters_TO_yards = answer.ToString
        End Function
        Public Function millimeters_TO_meters(ByVal answer As Decimal) As String
            Dim millimeters As Decimal = nudCONxEnterNum.Value
            answer = millimeters * 0.001D
            millimeters_TO_meters = answer.ToString
        End Function
    
        ''''NOTE: Conversion "CENTIMETER to XX"_______________________________________
        Public Function centimeters_TO_millimeters(ByVal answer As Decimal) As String
            Dim centimeters As Decimal = nudCONxEnterNum.Value
            answer = centimeters * 10D
            centimeters_TO_millimeters = answer.ToString
        End Function
        Public Function centimeters_TO_inches(ByVal answer As Decimal) As String
            Dim centimeters As Decimal = nudCONxEnterNum.Value
            answer = centimeters * 0.3937007874D
            centimeters_TO_inches = answer.ToString
        End Function
        Public Function centimeters_TO_feet(ByVal answer As Decimal) As String
            Dim centimeters As Decimal = nudCONxEnterNum.Value
            answer = centimeters * 0.03280839895D
            centimeters_TO_feet = answer.ToString
        End Function
        Public Function centimeters_TO_yards(ByVal answer As Decimal) As String
            Dim centimeters As Decimal = nudCONxEnterNum.Value
            answer = centimeters * 0.010936132983D
            centimeters_TO_yards = answer.ToString
        End Function
        Public Function centimeters_TO_meters(ByVal answer As Decimal) As String
            Dim centimeters As Decimal = nudCONxEnterNum.Value
            answer = centimeters * 0.01D
            centimeters_TO_meters = answer.ToString
        End Function
        ''''NOTE: Conversion "INCHES to XX"_____________________________________________
        Public Function inches_TO_millimeters(ByVal answer As Decimal) As String
            Dim inches As Decimal = nudCONxEnterNum.Value
            answer = inches * 25.4D
            inches_TO_millimeters = answer.ToString
        End Function
        Public Function inches_TO_centimeters(ByVal answer As Decimal) As String
            Dim inches As Decimal = nudCONxEnterNum.Value
            answer = inches * 2.54D
            inches_TO_centimeters = answer.ToString
        End Function
        Public Function inches_TO_feet(ByVal answer As Decimal) As String
            Dim inches As Decimal = nudCONxEnterNum.Value
            answer = inches * 0.083333333333D
            inches_TO_feet = answer.ToString
        End Function
        Public Function inches_TO_yards(ByVal answer As Decimal) As String
            Dim inches As Decimal = nudCONxEnterNum.Value
            answer = inches * 0.027777777778D
            inches_TO_yards = answer.ToString
        End Function
        Public Function inches_TO_meters(ByVal answer As Decimal) As String
            Dim inches As Decimal = nudCONxEnterNum.Value
            answer = inches * 0.0254D
            inches_TO_meters = answer.ToString
        End Function
        ''''NOTE: Conversion "FEET to XX"_____________________________________________
        Public Function feet_TO_millimeters(ByVal answer As Decimal) As String
            Dim feet As Decimal = nudCONxEnterNum.Value
            answer = feet * 304.8D
            feet_TO_millimeters = answer.ToString
        End Function
        Public Function feet_TO_centimeters(ByVal answer As Decimal) As String
            Dim feet As Decimal = nudCONxEnterNum.Value
            answer = feet * 30.48D
            feet_TO_centimeters = answer.ToString
        End Function
        Public Function feet_TO_inches(ByVal answer As Decimal) As String
            Dim feet As Decimal = nudCONxEnterNum.Value
            answer = feet * 12D
            feet_TO_inches = answer.ToString
        End Function
        Public Function feet_TO_yards(ByVal answer As Decimal) As String
            Dim feet As Decimal = nudCONxEnterNum.Value
            answer = feet * 0.33333333333D
            feet_TO_yards = answer.ToString
        End Function
        Public Function feet_TO_meters(ByVal answer As Decimal) As String
            Dim feet As Decimal = nudCONxEnterNum.Value
            answer = feet * 0.3048D
            feet_TO_meters = answer.ToString
        End Function
        ''''NOTE: Conversion "YARDS to XX"_____________________________________________
        Public Function yards_TO_millimeters(ByVal answer As Decimal) As String
            Dim yards As Decimal = nudCONxEnterNum.Value
            answer = yards * 914.4D
            yards_TO_millimeters = answer.ToString
        End Function
        Public Function yards_TO_centimeters(ByVal answer As Decimal) As String
            Dim yards As Decimal = nudCONxEnterNum.Value
            answer = yards * 91.44D
            yards_TO_centimeters = answer.ToString
        End Function
        Public Function yards_TO_inches(ByVal answer As Decimal) As String
            Dim yards As Decimal = nudCONxEnterNum.Value
            answer = yards * 36D
            yards_TO_inches = answer.ToString
        End Function
        Public Function yards_TO_feet(ByVal answer As Decimal) As String
            Dim yards As Decimal = nudCONxEnterNum.Value
            answer = yards * 3D
            yards_TO_feet = answer.ToString
        End Function
        Public Function yards_TO_meters(ByVal answer As Decimal) As String
            Dim yards As Decimal = nudCONxEnterNum.Value
            answer = yards * 0.9144D
            yards_TO_meters = answer.ToString
        End Function
        ''''NOTE: Conversion "METERS to XX"_____________________________________________
        Public Function meters_TO_millimeters(ByVal answer As Decimal) As String
            Dim meters As Decimal = nudCONxEnterNum.Value
            answer = meters * 1000D
            meters_TO_millimeters = answer.ToString
        End Function
        Public Function meters_TO_centimeters(ByVal answer As Decimal) As String
            Dim meters As Decimal = nudCONxEnterNum.Value
            answer = meters * 100D
            meters_TO_centimeters = answer.ToString
        End Function
        Public Function meters_TO_inches(ByVal answer As Decimal) As String
            Dim meters As Decimal = nudCONxEnterNum.Value
            answer = meters * 39.37007874D
            meters_TO_inches = answer.ToString
        End Function
        Public Function meters_TO_feet(ByVal answer As Decimal) As String
            Dim meters As Decimal = nudCONxEnterNum.Value
            answer = meters * 3.280839895D
            meters_TO_feet = answer.ToString
        End Function
        Public Function meters_TO_yards(ByVal answer As Decimal) As String
            Dim meters As Decimal = nudCONxEnterNum.Value
            answer = meters * 1.0936132983D
            meters_TO_yards = answer.ToString
        End Function

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,008

    Re: FUNCTION: Length Conversion, is there an easier way?

    I would recommend that you choose a standard unit and then implement methods to convert every other unit to and from that one. Any conversion between any other two units is then a case of first converting from the source unit to the standard and then from the standard unit to the target unit.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,242

    Re: FUNCTION: Length Conversion, is there an easier way?

    I'd use arrays and a dictionary(of string, decimal), with a textbox, a combobox, ten labels, and a button:

    Code:
    Public Class Form1
    
        Private conversions As New Dictionary(Of String, Decimal)
        Private toUnits() As String = {"Millimeters", "Centimeters", "Meters", "Inches", "Feet", "Yards"}
        Private suffix() As String = {" mm", " cm", " m", " in", " ft", " yd"}
        Private unitLabels() As Label
        Private valueLabels() As Label
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            conversions.Add("Millimeters to Centimeters", 0.1D)
            conversions.Add("Millimeters to Inches", 0.03937007874D)
            conversions.Add("Millimeters to Feet", 0.003280839895D)
            conversions.Add("Millimeters to Yards", 0.0010936132983D)
            conversions.Add("Millimeters to Meters", 0.001D)
            conversions.Add("Centimeters to Millimeters", 10D)
            conversions.Add("Centimeters to Inches", 0.3937007874D)
            conversions.Add("Centimeters to Feet", 0.03280839895D)
            conversions.Add("Centimeters to Yards", 0.010936132983D)
            conversions.Add("Centimeters to Meters", 0.01D)
            conversions.Add("Inches to Millimeters", 25.4D)
            conversions.Add("Inches to Centimeters", 2.54D)
            conversions.Add("Inches to Feet", 0.083333333333D)
            conversions.Add("Inches to Yards", 0.027777777778D)
            conversions.Add("Inches to Meters", 0.0254D)
            conversions.Add("Feet to Millimeters", 304.8D)
            conversions.Add("Feet to Centimeters", 30.48D)
            conversions.Add("Feet to Inches", 12D)
            conversions.Add("Feet to Yards", 0.33333333333D)
            conversions.Add("Feet to Meters", 0.3048D)
            conversions.Add("Yards to Millimeters", 914.4D)
            conversions.Add("Yards to Centimeters", 91.44D)
            conversions.Add("Yards to Inches", 36D)
            conversions.Add("Yards to Feet", 3D)
            conversions.Add("Yards to Meters", 0.9144D)
            conversions.Add("Meters to Millimeters", 1000D)
            conversions.Add("Meters to Centimeters", 100D)
            conversions.Add("Meters to Inches", 39.37007874D)
            conversions.Add("Meters to Feet", 3.280839895D)
            conversions.Add("Meters to Yards", 1.0936132983D)
    
            unitLabels = New Label() {Label1, Label2, Label3, Label4, Label5}
            valueLabels = New Label() {Label6, Label7, Label8, Label9, Label10}
    
            For x As Integer = 0 To 4
                unitLabels(x).Text = ""
                valueLabels(x).Text = ""
            Next
    
            ComboBox1.DataSource = toUnits
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim d As Decimal
            If Decimal.TryParse(TextBox1.Text, d) Then
                Dim counter As Integer = 0
                For Each u As String In toUnits
                    If u = ComboBox1.Text Then Continue For
                    unitLabels(counter).Text = u & ":"
                    valueLabels(counter).Text = (conversions(ComboBox1.Text & " to " & u) * d).ToString("N4") & suffix(Array.IndexOf(toUnits, u))
                    counter += 1
                Next
            End If
        End Sub
    
    End Class

  4. #4

    Thread Starter
    Lively Member CHEEZEnCRACKERS's Avatar
    Join Date
    Feb 2012
    Location
    Kentucky
    Posts
    67

    Re: FUNCTION: Length Conversion, is there an easier way?

    Nice, both ways seem to be much easier. I'll try them both.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width