The TStringsIterator object implements the CommonInterfaces.IIterator , CommonInterfaces.IObjectIterator, and CommonInterfaces.IStringIterator interfaces for an underlying TStrings Object. It provides an enumeration of all the items in a TStrings object or its descendants. It effectively hides the underlying implementation, in this case a TStrings, while still allowing access to all the items stored in the implementation.
The object provides functionality similar to an Enumeration in Java.
To use the iterator is simple, get the iterator as an IIterator, IObjectIterator, or IStringIterator interface and then keep calling the
NextElement method of the interface until the
HasMoreElements method returns False. The iterator works by giving you the next element until the end of the list, there is no provision for getting the previous element. The iterator manages its own memory and will free itself when it is completely dereferenced (as long as you use an interface to access it). The NextElement and CurrentElement methods of the IObjectIterator interface have been mapped to the
NextElementO and
CurrentElementO of this object; it is completely transparent to the user of the interface. Similarly, the
NextElementP and
CurrentElementP methods implement the NextElement and CurrentElement methods of the IIterator interface.
Note
|
The enumeration guarantees no ordering in the manner that the next element returns items. It just guarantees all items will be returned eventually, with no item appearing twice.
|
Namespace: RSInterfaceCollections
TInterfacedObject
RSInterfaceCollections.TStringsIterator
|
|
Name
|
Description
|
|
Create(TStrings)
|
Creates the enumeration and sets it to the beginning of the list.
|
Top
|
|
Name
|
Description
|
|
CurrentElement
|
Returns the current item in the enumeration (for CommonInterfaces.IStringIterator ), your "place" in the enumeration. When the iterator is first supplied or when it is Reset, there is no current element (calling this method in such a case would raise an exception). Only after the first call to the NextElement method will this method return a value. Unlike the NextElement method which returns a new value every time you call it, this method always returns the same value no matter how many times you call it (until NextElement is called, of course).
This method is part of the CommonInterfaces.IStringIterator interface.
Note
|
The CurrentElementO method implements the CurrentElement method for the IObjectIterator interface, and the CurrentElementP method implements the CurrentElement method for the IIterator interface.
|
|
|
CurrentElementO
|
Returns the current object item in the enumeration (for CommonInterfaces.IObjectIterator ), your "place" in the enumeration. It returns the Objects property of TStrings. When the iterator is first supplied or when it is Reset, there is no current element (calling this method in such a case would raise an exception). Only after the first call to the NextElementO method will this method return a value. Unlike the NextElementO method which returns a new value every time you call it, this method always returns the same value no matter how many times you call it (until NextElementO is called, of course).
This method is part of the CommonInterfaces.IObjectIterator interface.
Note
|
The CurrentElement method implements the CurrentElement method for the IStringIterator interface, and the CurrentElementP method implements the CurrentElement method for the IIterator interface.
|
|
|
CurrentElementP
|
Returns the current item (as a TRSPointerType) in the enumeration (for CommonInterfaces.IIterator), your "place" in the enumeration. It returns the Objects property of TStrings as a TRSPointerType. When the iterator is first supplied or when it is Reset, there is no current element (calling this method in such a case would raise an exception). Only after the first call to the NextElementPmethod will this method return a value. Unlike the NextElementP method which returns a new value every time you call it, this method always returns the same value no matter how many times you call it (until NextElementP is called, of course).
This method is part of the CommonInterfaces.IIterator interface.
Note
|
The CurrentElementO method implements the CurrentElement method for the IObjectIterator interface, and the CurrentElement method implements the CurrentElement method for the IStringIterator interface.
|
|
|
GetCount
|
Accessor method for the RSInterfaceCollections.TStringsIterator.Count property.
|
|
HasMoreElements
|
Returns True if there are more elements to retrieve from the enumeration. While this method returns true, the NextElement/ NextElementO/ NextElementP methods will not raise an exception.
This method is part of the CommonInterfaces.IIterator, CommonInterfaces.IObjectIterator , and CommonInterfaces.IStringIterator interfaces.
|
|
NextElement
|
Returns the current item in the enumeration (for CommonInterfaces.IStringIterator ), your "place" in the enumeration. When the iterator is first supplied or when it is Reset, there is no current element (calling this method in such a case would raise an exception). Only after the first call to the NextElement method will this method return a value. Unlike the NextElement method which returns a new value every time you call it, this method always returns the same value no matter how many times you call it (until NextElement is called, of course).
This method is part of the CommonInterfaces.IStringIterator interface.
Note
|
The CurrentElementO method implements the CurrentElement method for the IObjectIterator interface, and the CurrentElementP method implements the CurrentElement method for the IIterator interface.
|
|
|
NextElementO
|
Returns the next object item in the enumeration (Objects property of TStrings). If there are no more items, the method will raise an exception. To check if there are more items, use the HasMoreElements method. The NextElementO method advances its place in the enumeration and then returns that item. Every call to NextElementO returns a new item (until the end of the enumeration). If you wish to access the current item multiple times in a loop, use the CurrentElementO method.
This method is part of the CommonInterfaces.IObjectIterator interface.
|
|
NextElementP
|
Returns the next item in the enumeration (Objects property of TStrings as a TRSPointerType). If there are no more items, the method will raise an exception. To check if there are more items, use the HasMoreElements method. The NextElementP method advances its place in the enumeration and then returns that item. Every call to NextElementP returns a new item (until the end of the enumeration). If you wish to access the current item multiple times in a loop, use the CurrentElementP method.
This method is part of the CommonInterfaces.IIterator interface.
|
|
Reset
|
Resets the iterator to the beginning of the enumeration.
This method is part of the CommonInterfaces.IIterator, CommonInterfaces.IObjectIterator , and CommonInterfaces.IStringIterator interfaces.
|
Top
|
The following code shows how to use the iterator to get all the strings in a TStrings object:
Delphi
|
var
Items: IStringIterator;
begin
Items:= TStringsIterator.Create( ListBox1.Items );
while Items.HasMoreElements do
ShowMessage( Items.NextElement );
end;
|