Imports System
Imports System.Linq
Public Class UnitConverter
Private dicFormula As New Dictionary(Of String, List(Of Formula))
Sub New()
dicFormula.Add("mm2cm", New List(Of Formula) From {
New Formula() With {.SourceDesc = "mm2cm", .Ratio = 10, .Remark = "มิลลิเมตร 2 เซนติเมตร"}
})
dicFormula.Add("mm2m", New List(Of Formula) From {
New Formula() With {.SourceDesc = "mm2m", .Ratio = 1000, .Remark = "มิลลิเมตร 2 เมตร"}
})
dicFormula.Add("mm2km", New List(Of Formula) From {
New Formula() With {.SourceDesc = "mm2km", .Ratio = 1000000, .Remark = "มิลลิเมตร 2 กิโลเมตร"}
})
dicFormula.Add("cm2m", New List(Of Formula) From {
New Formula() With {.SourceDesc = "mm2cm", .Ratio = 100, .Remark = "มิลลิเมตร 2 เซนติเมตร"}
})
dicFormula.Add("cm2km", New List(Of Formula) From {
New Formula() With {.SourceDesc = "mm2m", .Ratio = 100000, .Remark = "มิลลิเมตร 2 เมตร"}
})
dicFormula.Add("m2km", New List(Of Formula) From {
New Formula() With {.SourceDesc = "mm2km", .Ratio = 1000, .Remark = "มิลลิเมตร 2 กิโลเมตร"}
})
End Sub
Public Function Calculate(ByVal n As Double, ByVal key As String) As Double
Dim iRet As Double = 0.0
Try
If dicFormula.ContainsKey(key) Then
iRet = n / dicFormula.Item(key)(0).Ratio 'dicFormula.TryGetValue(key, b)
End If
Catch ex As Exception
'1/0 --> Infinity (Programmer Error)
End Try
Return iRet
End Function
Private Class Formula
Sub New()
End Sub
Public Property SourceDesc As String
Public Property Ratio As Double = 1.0
Public Property Remark As String
End Class
'ประเภทการคำนวณ อทิเช่น ระยะทาง/ปริมาตร/etc.
Public Class UnitModel
Public Property CategoryID As Integer 'ประเภทการคำนวณ
Public Property Code As String 'ชื่อ
Public Property Description As String
Sub New(ByVal s As String, desc As String)
Code = s
Description = desc
End Sub
End Class
End Class