Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'Example Usage
- Dim myConvertedUnit : Set myConvertedUnit = ConvertDataUnit(4096, DataUnit_Bytes, DataUnit_Auto, 2)
- Msgbox myConvertedUnit.ToString, vbOkOnly, "Data Unit Usage Example"
- '----------------------------------------------------------------------------------------------------
- '''<summary>Defines constants for digital information size units as defined by the IEC.</summary>
- Public Const DataUnit_Auto = -1
- Public Const DataUnit_Bytes = 0
- Public Const DataUnit_Kibibytes = 1
- Public Const DataUnit_Mebibytes = 2
- Public Const DataUnit_Gibibytes = 3
- Public Const DataUnit_Tebibytes = 4
- Public Const DataUnit_Pebibytes = 5
- Public Const DataUnit_Exbibytes = 6
- Public Const DataUnit_Zebibytes = 7
- Public Const DataUnit_Yobibytes = 8
- '''<summary>Converts a value of DISU to another DISU</summary>
- '''<param name="dSize">The size of DISU</param>
- '''<param name="iCurrentUnit">The current DISU</param>
- '''<param name="iToUnit">The DISU to convert the size to</param>
- '''<param name="iPrecision">How many digits you want to the right of the decimal point after the conversion</param>
- '''<remarks>DISU stands for Digital Information Storage Unit</remarks>
- Public Function ConvertDataUnit(ByVal dSize, ByVal iCurrentUnit, ByVal iToUnit, ByVal iPrecision)
- Dim dConvertedValue : dConvertedValue = 0
- Dim iConvertedUnit : iConvertedUnit = 0
- Dim dBaseBytes
- If iCurrentUnit >= 0 And iCurrentUnit <= 8 Then
- dBaseBytes = dSize * (1024 ^ iCurrentUnit)
- Select Case True
- Case iToUnit >= 0 And iToUnit <= 8
- dConvertedValue = Round(dBaseBytes / (1024 ^ iToUnit), iPrecision)
- iConvertedUnit = iToUnit
- Case Else
- Do While dBaseBytes >= 1024
- dBaseBytes = dBaseBytes / 1024
- iConvertedUnit = iConvertedUnit + 1
- Loop
- dConvertedValue = Round(dBaseBytes, iPrecision)
- End Select
- Set ConvertDataUnit = New DataUnit.Init(dConvertedValue, iConvertedUnit)
- Else
- Err.Raise 5, "ConvertDataUnit(ByVal dSize, ByVal iCurrentUnit, ByVal iToUnit, ByVal iPrecision)", "An invalid unit of measure was given"
- End If
- End Function
- '''<summary>Represents a Digital Information Storage Unit</summary>
- Class DataUnit
- Private mValue
- Private mUnit
- Public Property Get Value()
- Value = mValue
- End Property
- Public Property Let Value(ByVal dValue)
- mValue = dValue
- End Property
- Public Property Get Unit()
- Unit = mUnit
- End Property
- Public Property Let Unit(ByVal iUnit)
- If iUnit < 0 Or iUnit > 8 Then
- Err.Raise 5, "DataUnit.Unit(ByVal iUnit)", "An invalid unit of measure was given"
- iUnit = 0
- End If
- mUnit = iUnit
- End Property
- Private Sub Class_Initialize()
- mValue = 0
- mUnit = 0
- End Sub
- Public Function Init(ByVal dValue, ByVal iUnit)
- mValue = dValue
- Unit = iUnit
- Set Init = Me
- End Function
- Public Function ToString()
- Dim aUnitLabel : aUnitLabel = Array("Bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB")
- ToString = mValue & " " & aUnitLabel(mUnit)
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment