SetFieldWidths Method


SetFieldWidths Method

Location

TextFieldParser.SetFieldWidths

Syntax

     Dim fileParser As FileIO.TextFieldParser     ' ...later... fileParser.SetFieldWidths(setOfWidths) 


setOfWidths (required; Integer array)

An array of the Integer values that each indicate the character length of a positional field within a fixed-width text file being parsed by a TextFieldParser object. All field widths must be greater than zero, although the last array element may be less than or equal to zero to indicate a final variable-width field.

Description

The SetFieldWidths method sets the number of characters used for each field in fixed-width text file parsing. This method also sets the TextFieldParser object's TextFieldType property to Microsoft.VisualBasic.FileIO.FieldType.FixedWidth. The first field begins with the first character on each record line, and subsequent fields immediately follow the fields before. You can indicate that the last field is of variable length (that is, it includes all characters until the end of the line) by setting the last field width to -1.

You can use the TextFieldParser object's FieldWidths property to set the field widths as well, but that property does not alter the TextFieldType property.

Usage at a Glance

  • The SetFieldWidths method is only useful with fixed-width input files, not delimited files.

  • An exception is thrown if you assign a zero or negative value to any field width other than the last one.

  • If the file being parsed only uses a single fixed-width column, you can pass a standard integer with that width to the SetFieldWidths method.

  • You must close the TextFieldParser object when finished with it. Use the object's Close method or create the object instance with the Using keyword. See the TextFieldParser Object entry in this chapter for an example.

Example

The following example uses the SetFieldWidths method to indicate that each line in the input file contains three fields: a 3-character field, a 30-character field, and a 5-character field.

     Dim scanInput As Microsoft.VisualBasic.FileIO.TextFieldParser     ' ...later...     scanInput.SetFieldWidths(New Integer(  ) {3, 30, 5}) 

Related Framework Entries

  • Microsoft.VisualBasic.FileIO.TextFieldParser.SetFieldWidths Method

See Also

CommentTokens Property, Delimiters Property, FieldWidths Property, ReadFields Method, SetDelimiters Method, TextFieldParser Object, TextFieldType Property




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net