Share via


VB.Net - OOP Metric Unit Converter

Overview

This is an OOP Metric Unit Converter application. There are five types of converter, which are selected via a ComboBox, and the Properties are edited and displayed via a PropertyGrid Control.

The five converters

  • Distance
  • Area
  • Volume
  • Mass
  • Time

Each of these converter Classes implements the Converter Interface, which allows a Property of type Converter to hold an instance of any of the five Classes. The Converter Interface doesn't contain any Methods or Properties, as the five Class types don't contain any common Methods or Properties, but using the Interface allows substitution.

This is the custom ListItem Class used to add a new instance of each of the Classes to the ComboBox.

Imports Unit_Converter
 
Public Class  ListItem
 
    Public Sub  New(classObject As Converter)
        _ClassObject = classObject
    End Sub
 
    Private _ClassObject As Converter
    Public Property  ClassObject As  Converter
        Get
            Return _ClassObject
        End Get
        Set(value As  Converter)
            _ClassObject = value
        End Set
    End Property
 
    Public Overrides  Function ToString() As String
        Return _ClassObject.GetType().Name
    End Function
 
End Class

The Distance Class

This class contains five Properties.

  • miles
  • km
  • m
  • cm
  • mm

When any of these Properties is edited in the PropertyGrid Control, the other Property values are calculated in the calculateFromMetres Sub-procedure...

Private Sub  calculateFromMetres(m As Decimal)
    _miles = CDec(m / 1609.34)
    _km = m / 1000
    _m = m
    _cm = m * 100
    _mm = m * 1000
End Sub

The Area Class

This class contains five Properties.

  • kmSquared
  • hectares
  • mSquared
  • cmSquared
  • mmSquared

Again when any of these Properties is edited in the PropertyGrid Control, the other Property values are calculated, this time in the calculateFromMetresSquared Sub-procedure...

Private Sub  calculateFromMetresSquared(ms As Decimal)
    _kmSquared = ms / 1000000
    _hectares = ms / 10000
    _mSquared = ms
    _cmSquared = ms * 10000
    _mmSquared = ms * 1000000
End Sub

The Volume Class

This class contains five Properties...

  • mCubed
  • litres
  • cl
  • cmCubed
  • mmCubed

In the familiar style of this application, when any of these Properties is edited in the PropertyGrid Control, the other Property values are calculated, this time in the calculateFromLitres Sub-procedure...

Private Sub  calculateFromLitres(ltrs As Decimal)
    _mCubed = ltrs / 1000
    _litres = ltrs
    _cl = ltrs * 100
    _cmCubed = ltrs * 1000
    _mmCubed = ltrs * 1000000
End Sub

The Mass Class

This class contains eight Properties...

  • tonnes
  • kg
  • hg
  • dag
  • g
  • dg
  • cg
  • mg

When any of these Properties is edited in the PropertyGrid Control, the other Property values are calculated, this time in the calculateFromGrams Sub-procedure...

Private Sub  calculateFromGrams(g As Decimal)
    _tonnes = g / 1000000
    _kg = g / 1000
    _hg = g / 100
    _dag = g / 10
    _g = g
    _dg = g * 10
    _cg = g * 100
    _mg = g * 1000
End Sub

The Time Class

The final Converter Class also has eight Properties...

  • weeks
  • days
  • hours
  • minutes
  • seconds
  • milliseconds
  • microseconds
  • nanoseconds

The calculator Method in this Class is the calculateFromSeconds Sub-procedure...

Private Sub  calculateFromSeconds(s As Decimal)
    _weeks = s / (7 * 24 * 60 * 60)
    _days = s / (24 * 60 * 60)
    _hours = s / (60 * 60)
    _minutes = s / 60
    _seconds = s
    _milliseconds = s * 1000
    _microseconds = s * 1000000
    _nanoseconds = s * 1000000000
End Sub

Conclusion

Using appropriate Controls in your applications, you're able to create compact, user friendly programs. Using minimal application memory resources, makes applications that load faster, and run faster. A combination of OOP techniques and minimal Control usage makes programs that can be easily understood by other programmers who might have occasion to update your program. Programs that are easily read are a must in a working environment.

Download

Download here...