views:

14

answers:

1

I have this as my conversion program for the "Length", how can I do it the simpliest way instead of keeping the if, elseif, else too much, i do not have much experience and trying to improve my programming skills on visual studio 2008.

Basically, I get annoyed with the formulas because I don't know if it is right, I use google but doesn't help because i don't know how to get it right when the program converts from type to type.

Public Class Form2
    Dim Metres As Integer
    Dim Centimetres As Integer
    Dim Inches As Integer
    Dim Feet As Integer
    Dim Total As Integer

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ErrorMsg.Hide()
    End Sub

    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click

        Metres = 1
        Centimetres = 0.01
        Inches = 0.0254
        Feet = 0.3048

        txtTo.Text = 0

        If txtFrom.Text <> "" Then
            If IsNumeric(txtFrom.Text) And IsNumeric(txtTo.Text) Then

                If cbFrom.Text = "Metres" And cbTo.Text = "Centimetres" Then
                    Total = txtFrom.Text * Metres
                    txtTo.Text = Total
                ElseIf cbFrom.Text = "Metres" And cbTo.Text = "Inches" Then
                    Total = txtFrom.Text * 100
                    txtTo.Text = Total
                ElseIf cbFrom.Text = "Metres" And cbTo.Text = "Feet" Then

                ElseIf cbFrom.Text = "Centimetres" And cbTo.Text = "Metres" Then

                ElseIf cbFrom.Text = "Centimetres" And cbTo.Text = "Inches" Then

                ElseIf cbFrom.Text = "Centimetres" And cbTo.Text = "Feet" Then

                ElseIf cbFrom.Text = "Inches" And cbTo.Text = "Metres" Then

                ElseIf cbFrom.Text = "Inches" And cbTo.Text = "Centimetres" Then

                ElseIf cbFrom.Text = "Inches" And cbTo.Text = "Feet" Then

                ElseIf cbFrom.Text = "Feet" And cbTo.Text = "Metres" Then

                ElseIf cbFrom.Text = "Feet" And cbTo.Text = "Centimetres" Then

                ElseIf cbFrom.Text = "Feet" And cbTo.Text = "Inches" Then

                End If

            End If
        End If
    End Sub
End Class

This is the source for what I have done at the moment.

A: 

One basic approach would be to split the handling of the "from"- and the "to"-conversion by using an intermediate format.

So the program code would be similar to something like this:

Dim intermediate as Double //Intermediate format in centimeters
Dim result as Double
Dim inputValue as Double = cDbl(txtFrom.Text)

If cbFrom.Text = "Metres" Then
   intermediate = inputValue  * 100
else if cbFrom.Text = "Centimetres" Then
   intermediate = inputValue  
else if 
   ...
End If

If cbTo.Text = "Metres" Then
  result = intermediate / 100
else if cbTo.Text = "Centimetres" Then
  result = intermediate
End If 

txtTo.Text = Math.Round(result, 2) //Round optional :)

Using this approach you only have to worry about conversion to and from your intermediate format and you only have to handle each input and output format once (instead of writing custom conversions for each input/output pair).

AUROnline