VBFixedString Attribute


VBFixedString Attribute

Class

Microsoft.VisualBasic.VBFixedStringAttribute

Applies To

Field

Constructor

     New(length) 


length (required; Integer)

The length of the string

Properties


Length (Integer)

Read-only. Value from the length constructor parameter.

Description

The <VBFixedString> attribute identifies a fixed-length string. It is the rough equivalent of the VB 6 declaration:

     Dim sFixed As String * length 

This attribute can be used to define fixed-length strings within structures, particularly structures that are to be passed to Win32 API functions. It is also useful when defining fixed-length strings to be written to, and read from, random access files.

Example

This example creates a random access file, which must contain fixed-length records, and uses the <VBFixedString> attribute to create a fixed-length string of 10 characters. This ensures that all records will be a uniform length. Without the <VBFixedString> attribute, the code would generate runtime errors (IOException) due to the invalid record length.

     ' Assumes "Option Strict Off"     Structure Person        <vbFixedString(10)> Public Name As String        Public Age As Short     End Structure     Public Sub BuildFile(  )        Dim onePerson As New Person        Dim outputFile As Integer = FreeFile(  )        FileOpen(outputFile, ".\person.txt", OpenMode.Random, _           OpenAccess.ReadWrite, OpenShare.Default, Len(onePerson))        onePerson.Name = "John"        onePerson.Age = 31        FilePut(outputFile, onePerson, 1)        onePerson.Name = "Jane"        onePerson.Age = 27        FilePut(outputFile, onePerson, 2)        FileGet(outputFile, onePerson, 1)        Console.WriteLine(Trim(onePerson.Name) & " is " & onePerson.Age)        FileClose(outputFile)     End Sub 




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