Question

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!

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top