0

我正在研究单位转换模块。我在这里以及在 CodeProject 上发现了几个好主意。我的代码看起来与http://www.codeproject.com/KB/cs/Unit_Conversion_Sample.aspx上的这个 C# 代码非常相似 从下面你可能会发现我对编程很陌生:)

我创建了一个 Units 基类,我继承它来创建每个单元类型。

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。这样,如果 Unit 对象的值为 1,类型为 Inch,并且类型更改为 Cm,则该值将更新为 2.54。

像这样的东西......我已经看到了这样的例子,但这里的不同之处在于,我不能在我的基类中指定 Covert 函数,因为它会随着我创建的每个新 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

我尝试使 Property UnitType Overridable 并在我创建的每个 UnitClass 中为 UnitType 创建一个新的 Override 属性,但我无法让它工作。

任何建议都非常感谢。非常感谢!

4

1 回答 1

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 只需更改现有对象上的值,而不是创建新对象。

我仍然有兴趣听到其他关于单位转换的想法......

于 2011-09-14T16:30:44.990 回答