【问题标题】:Measurements Unit Conversion - Structure of Class in VB.net测量单位转换 - VB.net 中的类结构
【发布时间】:2011-09-14 03:16:02
【问题描述】:

我正在开发一个单位转换模块。我在这里以及在 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 属性,但我未能使其正常工作。

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

【问题讨论】:

  • 我找到了一种方法来做到这一点,但由于我还没有足够的声誉,我不能再过 8 个小时来自我回答。简而言之,我稍微重新排列了我的代码并使用了一个返回布尔值 True 或 False 的函数,在该函数中,我将 UnitValue = 设置为转换后的 UnitValue,然后将 UnitType = 设置为新选择的类型。我会尽快发布我的代码。

标签: vb.net class unit-conversion


【解决方案1】:

我正在回答我自己的问题,因为我找到了一种通过稍微重新排列我的代码来完成更新 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 只需更改现有对象上的值,而不是创建一个新对象。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-03
    • 2012-02-06
    • 2018-02-01
    • 1970-01-01
    • 2010-10-04
    • 2013-06-09
    • 2012-11-23
    • 2020-10-14
    相关资源
    最近更新 更多