TextFieldParser.FieldWidths Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Denotes the width of each column in the text file being parsed.

Namespace:   Microsoft.VisualBasic.FileIO
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public int[] FieldWidths { get; set; }

Property Value

Type: System.Int32[]

An integer array that contains the width of each column in the text file that is being parsed.

Exception Condition

A width value in any location other than the last entry of the array is less than or equal to zero.

This property is meaningful only if the TextFieldType property is set to FieldType.FixedWidth. If the last entry in the array is less than or equal to zero, the field is assumed to be of variable width.

The SetFieldWidths method can also be used to set field widths.

The following table lists examples of tasks involving the FieldWidths property.



Read from a fixed-width text file

How to: Read From Fixed-width Text Files in Visual Basic

This example reads the file ParserText.txt, specifying the widths; the first column is 5 characters wide, the second is 10, the third is 11, and the fourth is of variable width

Using MyReader As New Microsoft.VisualBasic.FileIO.

    MyReader.TextFieldType = 
    MyReader.FieldWidths = {5, 10, 11, -1}
    Dim currentRow As String()
    While Not MyReader.EndOfData
            currentRow = MyReader.ReadFields()
            Dim currentField As String
            For Each currentField In currentRow
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & 
            "is not valid and will be skipped.")
        End Try
    End While
End Using

.NET Framework
Available since 2.0
Return to top