Class dùng cho Fixed Length Text String

3 225 0
Class dùng cho Fixed Length Text String

Đang tải... (xem toàn văn)

Thông tin tài liệu

Class dùng cho Fixed Length Text String Có lẽ lập trình với Text String là công việc bạn thường làm nhất. Ðó là lý do tại sao hơn mười năm trước Microsoft Basic của Bill Gates rất thịnh hành vì nó có những String Functions rất tiện dụng như Left, Right và Mid. Có nhiều Database programs export data records dưới dạng Fixed Length (có chiều dài cố định) Text String. Khi một Text String là gồm rất nhiều Data fields concatenated lại với nhau như thế việc ghi nhớ mỗi Data field bắt đầu từ character thứ mấy là cả một vấn đề. Dưới đây là một VB6 Class dùng cho Fixed Length Text String để ta extract những SubStrings từ nó một cách dễ dàng và không bị lẫn lộn. Giả sử ta có những Text Strings như sau: 123456789012345678901234567890123456789012345678901234567890123456789012123 4567890121234567890123456789012345 Truong Tam Phong 25 Greenfield Rd Cabramatta, NSW 21669724 3645 9724 1324 ptruong@ozemail.com.au Le Thanh Long 24/426 King St Fairfield, NSW 2165 9425 2534 9425 7755 llong@bigpond.com.au Hàng thứ nhất bên trên là để dùng làm thước mẫu cho ta thấy mỗi hàng Text gồm có nhiều Data fields nằm kế tiếp nhau (concatenated). Ta có thể đặt tên các Data fields và xác định chiều dài (size) của mỗi Data field như dưới đây: Name,20 Address1,20 Address2,20 Phone,12 Fax,12 Email,25 Nếu viết program theo lối thông thường ta sẽ nói đến Data field Phone bằng cách dùng Function Mid, chẳng hạn như Mid(TextString,61,12) , 61 là Offset của Data field Phone. Tương tợ như vậy, nói đến Data field Email ta phải viết Mid(TextString,85,25), mà 85 là Offset của Data field Email. Viết Code kiểu nầy rất dễ lầm lẫn nếu ta dùng các Data fields nầy thường. Do đó ta có Class clsTextRecord. Sau khi instantiate một Object của clsTextRecord, ta chỉ cần cho nó một File chứa cấu trúc của các Data fields như những hàng nói trên, clsTextRecord sẽ tự động tính ra Offsets của tất cả các Data fields. Program nầy dùng Delimited String Class clsString để Tokenise những hàng text trong File nói trên. Dưới đây là Listing của Property Let StructureFileName của clsTextRecord : Public Property Let StructureFileName(ByVal vData As String) ' Read the info about Datafields and their sizes to create a collection of Datafields. Dim FileNum, TextLine, FName mvarStructureFileName = vData myLength = 0 ' Obtain an available File handle FileNum = FreeFile ' Open the Structure file as Input Open mvarStructureFileName For Input As FileNum Offset = 1 ' Initialise the Offset ' Create a collection of Datafields Set DataFields = New Collection Do While Not EOF(FileNum) ' Read a line from the Structure file Line Input #FileNum, TextLine ' Declare and instantiate an object of Delimited String class type clsString Dim aLine As New clsString ' Declare and instantiate an object of Class ClsSubString which has ' FieldName, Offset and Size Dim SubString As New ClsSubString ' Tokenise the string into Fieldname and the size of the data field aLine.Text = TextLine aLine.Delimiter = "," FName = aLine.TokenAt(1) SubString.FieldName = FName ' assign FieldName SubString.Size = Val(aLine.TokenAt(2)) ' assign field Size in character SubString.Offset = Offset ' assign offset of SubString in String ' Update Offset value for the next data field Offset = Offset + SubString.Size myLength = myLength + SubString.Size ' Add an SubString Object to the DataFields collection DataFields.Add Item:=SubString, Key:=FName Set SubString = Nothing Loop ' Fill the internal String with Blank spaces mvarTextStringData = Space(myLength) Close FileNum ' Close input file End Property Khi bạn chạy program mẫu kèm theo, trước hết nó instantiate một Object loại clsTextRecord, load các Test Text Strings từ file Persons.txt vào một Listbox tên lstInputString và đọc content của File chứa cấu trúc của các Data Fields tên PersonRecordStructure.txt. Listing của Sub Form_load như sau: Private Sub Form_Load() ' Instantiate an Object of ClsTextRecord Set ALongString = New ClsTextRecord ' Build the Collection of Datafields ALongString.StructureFileName = App.Path & "\PersonRecordStructure.txt" ' Load the test Strings into the ListBox PopulateListBoxFromFile lstInputString, "Persons.txt", False End Sub Chọn một item trong lstInputString rồi click nút CmdGetSubStrings. Program sẽ ngắc khúc selected Text String và display các Data fields trong những TextBoxes bên dưới. Nếu bạn chọn hàng đầu program sẽ display cho thấy mỗi Data field chứa bao nhiêu characters. Ngoài ra, ta cũng có thể dùng clsTextRecord để viết data trực tiếp vào mỗi Data field trong Fixed Length Text String. Lưu ý cách dùng tên các Data Fields trong listing của Sub CmdGetSubStrings_Click() như sau: Private Sub CmdGetSubStrings_Click() ' assign the selected string in the listbox to the ALongString object ALongString.TextStringData = lstInputString.Text ' Assign appropriate datafield data to the TextBoxes txtName.Text = ALongString.FieldData("Name") txtAddress1.Text = ALongString.FieldData("Address1") txtAddress2.Text = ALongString.FieldData("Address2") txtPhone.Text = ALongString.FieldData("Phone") txtFax.Text = ALongString.FieldData("Fax") txtEmail.Text = ALongString.FieldData("Email") End Sub Bạn có thể download source code của program mẫu nầy kể cả class clsTextRecord. . một VB6 Class dùng cho Fixed Length Text String để ta extract những SubStrings từ nó một cách dễ dàng và không bị lẫn lộn. Giả sử ta có những Text Strings. Class dùng cho Fixed Length Text String Có lẽ lập trình với Text String là công việc bạn thường làm nhất. Ðó

Ngày đăng: 22/10/2013, 11:15

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan