2011-09-14 35 views
0

我工作的單位轉換模塊類的結構。我在這裏找到了幾個好主意,以及CodeProject。我的代碼看起來非常類似於這個C#代碼http://www.codeproject.com/KB/cs/Unit_Conversion_Sample.aspx 從以下你可能會認識到我很新編程:)測量單位轉換 - 在VB.net

我創建了一個單位基類,我創建每個單元類型。

Public Class Units 
Private _unitvalue As Double 
Private _unittype As [Enum] 

Public Sub New(UnitValue As Double, UnitType As [Enum]) 
    _unitvalue = UnitValue 
    _unittype = UnitType 
End Sub 

Public Property UnitValue() As Double 
    Get 
     Return _unitvalue 
    End Get 
    Set(value As Double) 
     _unitvalue = value 
    End Set 
End Property 

Public Property UnitType() As [Enum] 
    Get 
     Return _unittype 
    End Get 
    Set(value As [Enum]) 
     _unittype = value 
    End Set 
End Property 

Public Overrides Function ToString() As String 
    Return String.Format("{0} {1}", UnitValue.ToString(), UnitType.ToString()) 
End Function 

End Class 

然後我繼承這個類,開始創建包含單位轉換函數的單位。

Public Class WeightUnit 
Inherits Units 
Enum WeightSym 
    'Pounds 
    Lbs 
    'Kilograms 
    Kg 
End Enum 
Sub New(UnitValue As Double, UnitType As WeightSym) 
    MyBase.New(UnitValue, UnitType) 
End Sub 
Public Function Convert(toUnit As WeightSym) As WeightUnit 
    'Base Weight Unit is Lbs 

    Dim fromUnit As WeightSym 
    fromUnit = UnitType 

    Dim Lbs As Double = 0 
    Select Case fromUnit 
     'Standard 
     Case WeightSym.Lbs 
      Lbs = UnitValue 
     Case WeightSym.Kg 
      Lbs = UnitValue * 2.2046226 
    End Select 

    Dim toVal As Double = 0 
    'to unit based on Lbs 
    Select Case toUnit 
     'Standard 
     Case WeightSym.Lbs 
      toVal = Lbs 
     Case WeightSym.Kg 
      toVal = Lbs * 0.4535924 
    End Select 
    Return New WeightUnit(toVal, toUnit) 
End Function 
End Class 

我需要創建幾種不同的單位類型,例如長度,壓力等。除了一個問題,這很適用。我希望能夠改變UNITTYPE,並自動更新UnitValue。使得如果單元對象具有1的值和類型英寸,並且類型被改變爲釐米,該值將更新到2.54。

事情是這樣的....我見過這樣的例子,但這裏的區別是,在我的基類,我不能指定隱蔽功能,因爲它與每個新UnitClass我創造變化。

Public Property UnitType() As [Enum] 
Get 
    Return _unittype 
End Get 
Set(value As [Enum]) 
    _unittype = value 
    _unitvalue = Convert(value).UnitValue 
End Set 
End Property 

我試圖使該物業U​​NITTYPE可重寫和我創建的每個UnitClass創造UNITTYPE新的替代屬性,但我沒能得到那個工作。

任何建議都非常讚賞。 謝謝了!

+0

我發現了一個辦法做到這一點,但我不能爲8個小時的自我答案,因爲我沒有足夠的聲譽然而。簡而言之,我重新排列了我的代碼並使用了一個返回布爾值True或False的函數,在該函數中,我將UnitValue =設置爲轉換後的UnitValue,然後將UnitType =設置爲新選擇的類型。我會盡我所能發佈我的代碼。 – GetFuzzy

回答

0

我回答我自己的問題,因爲我找到了一種方法來完成更新UnitValue和UnitType,稍微重新排列我的代碼。我把轉換函數分成兩部分。首先,我創建了一個只返回Double的WeightConversion函數。

Public Function WeightConversion(Quantity As Double,fromUnit As WeightSym,_ 
toUnit As WeightSym) As Double 
'Base Weight Unit is Lbs 

Dim fromUnit As WeightSym 
fromUnit = UnitType 

Dim Lbs As Double = 0 
Select Case fromUnit 
    'Standard 
    Case WeightSym.Lbs 
     Lbs = Quantity 
    Case WeightSym.Kg 
     Lbs = Quantity * 2.2046226 
End Select 

Dim toVal As Double = 0 
'to unit based on Lbs 
Select Case toUnit 
    'Standard 
    Case WeightSym.Lbs 
     toVal = Lbs 
    Case WeightSym.Kg 
     toVal = Lbs * 0.4535924 
End Select 
Return toVal 
End Function 

然後,我創建了一個功能,看起來像這樣......我在執行我在基類做一個重寫的功能,並與下面覆蓋它。

Public Function ToUnit(toType As LengthSym) As Boolean 
    'First Set UnitValue 
    Me.UnitValue = LengthConversion(Me.UnitValue, Me.UnitType, toType) 
    'Then Set the UnitType to the newly choosen unit 
    Me.UnitType = toType 
    Return True 
End Function 

這給了我正在尋找的效果......現在轉換爲另一個UnitType只是更改現有對象上的值而不是創建新對象。

我仍然有興趣聽單位轉換其他的想法...