开发者

Measurements Unit Conversion - Structure of Class in VB.net

I'm working on a Unit Conversion Module. I've found several good ideas here, as well as on CodeProject. My code looks very similar to this C# code at http://www.codeproject.com/KB/cs/Unit_Conversion_Sample.aspx From the following you'll probably gather that I'm pretty new to programming:)

I've created a Units base class that I inherit to create each unit type.

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

I then inherit this class to start creating Units with a Unit conversion function included.

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

I need to create several different Unit types, such as Length, Pressure, etc. This is working well except for one issue. I'd like to be able to change the UnitType, and automatically update the UnitValue. Such that if the Unit object has a value of 1 and a type of Inch, and the type is changed to Cm, the value would update to 2.54.

Something like this.... I've seen examples of this, but the difference here is that, I can't specify the Covert function in my base class because it changes with each new UnitClass I create.

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

I tried making the Property UnitType Overridable and creating a new Override Property for UnitType in each UnitClass that I created, but I failed to get that to work.

Any suggestions are much appreciated. Thanks much!


I'm answering my own questions as I found a way to accomplish updating the UnitValue and UnitType, by slightly rearranging my code. I broke the Convert Function out into two pieces. First I created a WeightConversion Function that just returns a Double.

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

Then, I created a function that looks like this... In my implementation I made an overridable function in my base class, and override it with the below.

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

This gave me the effect I was looking for... Now converting to another UnitType simply changes the values on the existing object instead of creating a new Object.

I'd still be interested in hearing other ideas for unit conversion...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜