|
-
May 3rd, 2016, 07:01 PM
#1
Thread Starter
Lively Member
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|