Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

TextFieldParser.FieldWidths Property 

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


' Usage
Dim value As Integer() = TextFieldParserObject.FieldWidths
' Declaration
Public Property FieldWidths As Integer()

Integer ().

The following condition may produce an exception:

  • A width value in any place other than the last entry of the array is less than or equal to zero (ArgumentException).

This property is meaningful only if TextFieldParser.TextFieldType Property = 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. For more information, see TextFieldParser.SetFieldWidths Method.

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

To See

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.TextFieldParser("C:\ParserText.txt")
    MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.FixedWidth
    MyReader.FieldWidths = New Integer() {5, 10, 11, -1}
    Dim currentRow As String()
    While Not MyReader.EndOfData
        Try
            currentRow = MyReader.ReadFields()
            Dim currentField As String
            For Each currentField In currentRow
                MsgBox(currentField)
            Next
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & _
            "is not valid and will be skipped.")
        End Try
    End While
End Using

Namespace: Microsoft.VisualBasic.FileIO

Class: TextFieldParser

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

No permissions are required.

Community Additions

ADD
Show:
© 2015 Microsoft