' ***********************************************************************
' Author : Elektro
' Modified : 04-February-2015
' ***********************************************************************
' <copyright file="FixedLengthString.vb" company="Elektro Studios">
' Copyright (c) Elektro Studios. All rights reserved.
' </copyright>
' ***********************************************************************
''' <summary>
''' Defines a <see cref="String"/> with a fixed length.
''' </summary>
Public NotInheritable Class FixedLengthString
#Region " Properties "
''' <summary>
''' Gets or sets the fixed string length.
''' </summary>
''' <value>The fixed string length.</value>
Public Property FixedLength As Integer
Get
Return Me.fixedLength1
End Get
Set(ByVal value As Integer)
Me.fixedLength1 = value
End Set
End Property
''' <summary>
''' The fixed string length.
''' </summary>
Private fixedLength1 As Integer
''' <summary>
''' Gets or sets the padding character thath fills the string.
''' </summary>
''' <value>The padding character thath fills the string.</value>
Public Property PaddingChar As Char
Get
Return Me.paddingChar1
End Get
Set(ByVal value As Char)
Me.paddingChar1 = value
End Set
End Property
''' <summary>
''' The padding character thath fills the string.
''' </summary>
Private paddingChar1 As Char
''' <summary>
''' Gets or sets the unmodified string.
''' </summary>
''' <value>The unmodified string.</value>
Public Property ValueUnfixed As String
Get
Return Me.valueUnfixed1
End Get
Set(ByVal value As String)
Me.valueUnfixed1 = value
End Set
End Property
''' <summary>
''' The unmodified string.
''' </summary>
Private valueUnfixed1 As String
''' <summary>
''' Gets the fixed string.
''' </summary>
''' <value>The fixed string.</value>
Public ReadOnly Property ValueFixed As String
Get
Return Me.FixLength(Me.valueUnfixed1, Me.fixedLength1, Me.paddingChar1)
End Get
End Property
#End Region
#Region " Constructors "
''' <summary>
''' Prevents a default instance of the <see cref="FixedLengthString" /> class from being created.
''' </summary>
Private Sub New()
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="FixedLengthString" /> class.
''' </summary>
''' <param name="value">The string value.</param>
''' <param name="fixedLength">The fixed string length.</param>
''' <param name="paddingChar">The padding character thath fills the string.</param>
Public Sub New(ByVal value As String,
ByVal fixedLength As Integer,
Optional ByVal paddingChar As Char = " "c)
Me.valueUnfixed1 = value
Me.fixedLength1 = fixedLength
Me.paddingChar1 = paddingChar
End Sub
#End Region
#Region " Public Methods "
''' <summary>
''' Returns a <see cref="System.String" /> that represents this instance.
''' </summary>
''' <returns>A <see cref="System.String" /> that represents this instance.</returns>
Public Overrides Function ToString() As String
Return Me.ValueFixed
End Function
#End Region
#Region " Private Methods "
''' <summary>
''' Fixes the length of the specified string.
''' </summary>
''' <param name="value">The string value.</param>
''' <param name="fixedLength">The fixed string length.</param>
''' <param name="paddingChar">The padding character thath fills the string.</param>
''' <returns>System.String.</returns>
Private Function FixLength(ByVal value As String,
ByVal fixedLength As Integer,
ByVal paddingChar As Char) As String
If (value.Length > fixedLength) Then
Return value.Substring(0, fixedLength)
Else
Return value.PadRight(fixedLength, paddingChar)
End If
End Function
#End Region
End Class