ClassMicrosoft.VisualBasic.VBFixedStringAttribute Applies ToField ConstructorNew(length)
Properties
DescriptionThe <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. ExampleThis 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 |