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

Source: Internet
Author: User
Tags error handling finally block

20.2.3 Treader Object

The Treader object is a filer object that can be instantiated to read data from the associated stream. The Treader object inherits from Tfiler and Treader declares many properties, methods, and events in addition to the inherited properties and methods from Tfiler.

The owner and parent properties are used to represent the owner and parent node of a part that is read from a stream in a reader object. Onerror,onfindmethod and Onsetname events enable applications to customize the response when they read data while running. In addition to overwriting some of the methods inherited from the Tfiler object, the Treader object also defines a large number of methods for reading different types of data and triggering events.

Properties and methods of 20.2.3.1 Treader objects

1. Owner Property

Statement: Property owner:tcomponent;

The owner property of the reader object stores the part that will be used to assign values to the Owner property of the part read from the stream in reader.

2. Parent Property

Statement: Property parent:tcomponent;

The Parent property store will be used to assign values to all controlled parent properties that are read from the stream of reader.

3. Position Properties

Statement: Propertion:longint;

The position property of the reader object represents the current position of the read in the associated stream. The value of the position should also include the size of the read buffer. For a reader object, the value of the position is greater than the position value of the stream. If the value of the position is set beyond the current buffer, the call to Flushbuffer is caused.

4. Beginreferences method

Statement: procedure Beginreferences;

The Beginreferences method initiates a series of commands for read parts that contain cross-references to each other. It is often used with fixupreferences and endreferences in the try...finally program block.

After the beginreferences is invoked, the reader object creates a list of all the objects and names that are read. After all the independent objects are read out, the Fixupreferences method is called to transfer the names from the stream to the object instance. Finally, the Endreferences method is invoked to release the list.

The processing parts refer to each other in the form of a program block as follows:

Beginreferences; {Create temporary list}

Try

{Read all the parts and put their names in a temporary list}

...

Fixupreferences; Decomposition

Finally

Endreferences; {Free Temporary list}

End

5. Fixupreferences method

Statement: procedure Fixupreferences;

The Fixupreferences method decomposes the reference relationships of various interdependent parts that are read out from the stream. Fixupreferences is always in the try...finally block and is used in conjunction with Beginreferences and Endreferences.

6. Endreferences method

Statement: procedure Endreferences;

The Endreferences method terminates processing a referenced block operation and releases the list of objects. It is always used in conjunction with Beginreferences and Fixupreferences.

7. Readlistbegin method

Statement: procedure Readlistbegin;

The Readlistbegin method reads the list start flag from the stream associated with the reader object. If the item to be read immediately in the stream is not a list start flag written by the Writelistbegin method, Readlistbegin will cause a read exception event.

Typically, after the Readlistbegin method is invoked, the loop that follows a read item loops back to the Endflist method to return true termination conditions. At this point, the next item in the stream is the list end flag, and the Readlistend method needs to be invoked.

8. Readlistend method

Statement: procedure Readlistend;

The Readlistend method reads the list end flag from the stream. If the item you are reading is not a list end flag, the Readlistend method raises a Ereaderror exception event.

9. Endoflist method

Statement: function Endoflist:boolean;

The Endoflist method returns True if the reader object reads the item list result flag.

The Tstrings object uses the Readlistbegin and Readlistend methods when reading a list of items from a reader object. The following readdata is a Tstrings method for reading string data in the defineproperties aspect.

Procedure Tstrings.readdata (Reader:treader);

Begin

Reader.readlistbegin; {Read list start flag}

Clear; {Clear Existing String}

While does {reader.endoflist do {as long as there is data ...}

ADD (reader.readstring); { ... Read a string and add it to the list}

Reader.readlistend; {Cross list End flag}

End

Readsignature method

Statement: procedure Readsignature;

The Readsignature method invokes the Readsignature method before reading the part from the stream. Detects the label before loading the object. Reader objects can prevent inadvertent reading of invalid or obsolete data. The filer tag is four characters, and for Delphi 2.0, the label is "TPF0".

Readprefix method

Disclaimer: Procedure Readprefix (var plags:tfilerflags; var achild, Pos:integer);

The function of the Readprefix method is very much like the readsignature, except that it is the symbol (PREFIX) that reads the front of the part in the stream. When a write object writes a part to a stream, it writes two values in front of the part, the first value indicating whether the part is a form that inherits from the ancestor form and whether it is important for its position in the form; the second value indicates the order in which it was created in the ancestor form. The Readcomponent method automatically invokes the Readprefix. However, you can also call this direction directly if you need to read the read-only flag of the part independently.

Onfindmethod Event

Statement: Property onfindmethod:tfindmethodevent;

Onfindmethod event, which occurs when the reader object reads the object's method pointer, the property is usually an event of the method pointer.

The reason for responding to Onfindmethod events is usually when the process cannot find a method. If the FindMethod method does not find a method specified by name, if it sets the error argument of the Onfindmethod method to True, it causes the Readerror exception event, or instead, sets the error argument to False. Prevents the FindMethod method from throwing an exception event.

Error method

Statement: function Error (const message:string): Boolean; Virtual

The error method is defined in the protected part of the reader object, which is the onerror event for the reader object. Its return value determines whether the error-handling process continues. If the return value is true, it means that the program should continue error handling, and if the return value is False, the error condition is ignored.

If there is an error reading a part or property. The reader object invokes the error method. By default, error sets the return value to False, and then calls the OnError event-handling procedure.

The Treader object is always in the except part of the TRY...EXCEPT program block and provides the opportunity for the user to ignore the error. The use of the error is as follows:

... {Read part}

Except

On E:exception do

Begin

... {Perform some cleanup actions}

If Error (e.message) then raise;

End

End

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.