title: Measurements Unit Conversion - Structure of Class in VB.net
body: <p>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 <a href="http://www.codeproject.com/KB/cs/Unit_Conversion_Sample.aspx" rel="nofollow">http://www.codeproject.com/KB/cs/Unit_Conversion_Sample.aspx</a> 
  From the following you'll probably gather that I'm pretty new to programming:)  </p>
  
  <p>I've created a Units base class that I inherit to create each unit type.  </p>
  
  <pre><code>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
  </code></pre>
  
  <p>I then inherit this class to start creating Units with a Unit conversion function included.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <p>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.    </p>
  
  <pre><code>Public Property UnitType() As [Enum]
  Get
      Return _unittype
  End Get
  Set(value As [Enum])
      _unittype = value
      _unitvalue = Convert(value).UnitValue
  End Set
  End Property
  </code></pre>
  
  <p>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.  </p>
  
  <p>Any suggestions are much appreciated. 
  Thanks much!</p>