2010-05-24 5 views
1

"길이"에 대한 변환 프로그램으로이 프로그램이 있는데, if, elseif, else를 너무 많이 유지하는 대신 가장 간단한 방법으로 어떻게 수행 할 수 있습니까? 많은 경험이 없으며 개선하기 위해 노력하고 있습니다. 비주얼 스튜디오에서 내 프로그래밍 기술 2008.Visual Studio Conversion Suite

기본적으로, 내가 옳은지 모르겠다 때문에 공식적으로 짜증이 난, 내가 사용하는 방법을 잘 모르겠지만 도움이되지 않습니다. 프로그램이 유형에서 유형으로 변환 할 때

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 

이것은 현재 내가 한 작업의 출처입니다.

답변

0

하나의 기본 접근법은 "중간"형식을 사용하여 "변환"및 "변환"변환의 처리를 분리하는 것입니다. 만에 당신의 중간 형식에서 변환에 대해 걱정할 필요가 당신은 각 입력 및 출력 형식을 처리해야

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 :) 

이 방법을 사용 :

그래서 프로그램 코드는 다음과 같이 유사하다 한 번 (각 입력/출력 쌍에 대한 사용자 정의 변환을 작성하는 대신).

관련 문제