The 20th Chapter-Development Delphi Object Type Data management function (four) (2)

Source: Internet
Author: User
Tags integer integer numbers

OnError incident

Statement: Property onerror:treadererror;

The OnError event is raised when the reader object reads the data in error. By handling OnError events, you can selectively handle or ignore errors.

The last parameter passed to the OnError event handler is the Var parameter named handled. By default, the error method resets handled to true. This will prevent errors from being processed further. If the event-handling process still handled the False,reader object, a Ereaderror exception event is raised.

SetName method

Statement: Procedure SetName (component:tcomponent; var name:string virtual);

The SetName method allows the reader object to modify the name value before assigning the name value of the part that is read from the stream to the part's Name property. The Readcomponent method reads the type and first name of the part before reading the property value and other data of the part after reading the name, readcomponent the read name to Setname,name is a var argument. Therefore setname can modify the string value before returning. SetName also invokes the Onsetname event handler, which passes the name string as the Var parameter into the event-handling process, so the event-handling process can also modify the string's value.

Onsetname incident

Statement: Property onsetname:tsetnameevent;

Onsetname event occurs before the Read object sets the Name property of a part, the var parameter name parameter of the Onsetname event process is a var parameter, so the event-handling procedure can modify the value of name before assigning name to the part. This is helpful when you want to filter the names of parts in a form.

The following Onsetname event handler named the part containing the "button" in the name and replaced it with "pushbutton".

Procedure Tform1.readersetname (Reader:treader; Component:tcomponent;

var name:string);

Var

Buttonpos:integer;

Begin

Buttonpos: = Pos (' Button ', Name);

If Buttonpos <> 0 Then

Name: = Copy (name, 1, ButtonPos-1) + ' pushbutton ' +

Copy (name, Buttonpos + 6, Length (name));

End

ReadValue method

Statement: function Readvalue:tvaluetype;

The ReadValue method reads the type of the tight item in the stream, and after the function returns, the stream's pointer moves after the value type indicator.

Tvaluetype is an enumeration type. Each item stored in the stream of the Filer object has a byte that identifies the type of the item and reads the byte before each item is read to guide which method is invoked to 闱 the item. The value of this byte is one of the value types defined by Tvaluetype.

NextValue method

Statement: function Nextvalue:tvaluetype;

The function of the NextValue method is also to return the type of item that is immediately followed in the reader object stream, which differs from readvalue in that it does not move the pointer position.

Readboolean method

Statement: function Readboolean:boolean;

The Readboolean method reads a Boolean value from the stream of the reader object and moves the stream position pointer accordingly.

20, Readchar method

Statement: function Readchar:char;

The Readchar method reads a character from the stream of the reader object.

Readfloat method

Statement: function readfloat:extended;

The Readfloat method reads floating point numbers from the stream.

Readident method

Statement: function readident:string;

The Readident method reads the identifier from the stream.

Readinteger method

Statement: function Readinteger:longin

The Readinteger method reads integer numbers from the stream.

24.ReadString method

Statement: function Read string:string;

The ReadString method reads a string from the stream of the reader object and returns the contents of the string. The string is written by the WriteString method of the writer object.

Implementation of 20.2.3.2 Treader object

The main function of the Filer object is that Delphi is used to read and write various types of data (including part objects) in the DFM file. One of the essential features of these data is that it is longer, and the Filer object abstracts the operation of Read and write data, wrapping the object to provide a large number of reading and writing methods to facilitate the program's invocation. Therefore, in the application can be widely made filer objects, fully utilize Delphi object-oriented technology. Moreover, the Filer object is bundled with the stream object, on the one hand, it can access the data in any format in various storage mediums, on the other hand, because of the full use of object-oriented dynamic Binder, various methods of reading and writing are consistent, so the method invocation is simple. Here we focus on the implementation of the properties and methods associated with reading and writing data in the reader object.

1. Implementation of Treader property

In the realization of Treader object's attribute, we introduce the implementation of position emphatically.

The position attribute is defined by the use of read-write controls, which are getposition and SetPosition methods respectively.

TReader = Class (Tfiler)

Private

...

function Getposition:longint;

Procedure SetPosition (Value:longint);

Public

...

Property Position:longint read GetPosition write setposition;

End

The read-write control method for Postition is as follows:

function TReader.GetPosition:Longint;

Begin

Result: = Fstream.position + Fbufpos;

End

Procedure Treader.setposition (Value:longint);

Begin

Fstream.position: = Value;

Fbufpos: = 0;

Fbufend: = 0;

End

Fbufpos and Fbufend variables are described in the Treader parent object Tfiler object. A buffer fbufpos within the Filer object that is assigned a bufsize size is the relative position in the buffer, and fbufend refers to the position at the end of the data in the buffer (the data in the buffer does not necessarily fill the entire buffer).

In the GetPosition method, you can see that the position value of the reader object and the position value of the stream object are different. The reader object has one more fbutpos.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.