Fixed Field Lengths

Josaph

Member
Joined
Jan 4, 2007
Messages
20
Location
Illinois
Programming Experience
5-10
In a majority of the projects I need to export data in a fixed field length style with no delimiters. When I switched over to .NET I noticed this capability was removed.

I am hoping that I can get some constructive feedback on this example I have put together to see if it's an optimal solution for creating fixed field variables in VB.NET. Forgive me if this was already discussed, but I didn't see it on the most recent articles when I searched for "fixed length".

To test the code all you need to do is add a Button to a Form and leave it named Button1. Then just copy and paste the following code to your VB.NET application under the Form1's code. I appreciate any constructive feedback on this.

VB.NET:
    'This is my fixed string structure which handles the process of ensuring
    'that the string does not exceed the maximum length or pads it with spaces
    'if it is smaller than the maximum size.
    Private Structure FixedString
        Private MaxLen As Integer, StrVal As String

        Public Property Length() As Integer
            Get
                Return MaxLen
            End Get
            Set(ByVal Value As Integer)
                MaxLen = Value
            End Set
        End Property

        Public Property Value() As String
            Get
                Return StrVal
            End Get
            Set(ByVal Value As String)
                StrVal = FixLength(Value)
            End Set
        End Property

        'This is my FixLength Function. It checks to see if the string
        'is the same length, less than, or greather than the maximum
        'length. Based on that, it either trims the string down, or pads
        'it with spaces.
        Private Function FixLength(ByVal SourceString As String)
            Dim rtnString As String = ""
            If SourceString Is Nothing Then
                SourceString = ""
            End If
            If MaxLen > 0 Then
                Select Case Len(SourceString)
                    Case MaxLen
                        rtnString = SourceString
                    Case Is < MaxLen
                        rtnString = SourceString.PadRight(MaxLen)
                    Case Is > MaxLen
                        rtnString = Mid(SourceString, 1, MaxLen)
                End Select
            Else
                rtnString = SourceString
            End If
            Return rtnString
        End Function
    End Structure

    'This is my example structure for defining Client information.
    Private Structure ClientInfo
        Dim FirstName As FixedString
        Dim LastName As FixedString
        Dim MidInit As FixedString
    End Structure

    'I just added a single button to a form to run the test.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim clInfo As ClientInfo
        With clInfo
            'Define maximum lengths
            .FirstName.Length = 5
            .MidInit.Length = 1
            .LastName.Length = 10

            'Assign values
            .FirstName.Value = "Johnathon"
            .MidInit.Value = "Anderson"
            .LastName.Value = "Doe"

            'Print to Console
            Console.WriteLine("*" & .FirstName.Value & " " & .MidInit.Value & " " & .LastName.Value & "*")
        End With
    End Sub
 
VB.NET:
 Function MakeFixedLength(ByVal str As String, ByVal desiredLength As Integer, ByVal LR As String, ByVal chr As Char) As String
        If str.Length >= desiredLength Then
            Return str.Substring(0, desiredLength)
        Else
            If LR = "L" Then
                Return str.PadLeft(desiredLength, chr).Substring(0, desiredLength)
            Else
                Return str.PadRight(desiredLength, chr).Substring(0, desiredLength)
            End If
        End If
    End Function
 
Back
Top