关于网友提出的“ 你们好,我想问一下,vb 中的 For Each in 循环语句,delphi 中是那条语句呢?请帮助。”问题疑问,本网通过在网上对“ 你们好,我想问一下,vb 中的 For Each in 循环语句,delphi 中是那条语句呢?请帮助。”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 你们好,我想问一下,vb 中的 For Each in 循环语句,delphi 中是那条语句呢?请帮助。
描述: 你们好,我想问一下,vb 中的 For Each ....in.... 循环语句,delphi 中是那条语句呢?请帮助。
解决方案1: 看帮助文档么,呵呵
Iteration Over Containers Using For statements
Both Delphi for .NET and for Win32 support for-element-in-collection style iteration over containers. The following container iteration patterns are recognized by the compiler:
for Element in ArrayExpr do Stmt;
for Element in StringExpr do Stmt;
for Element in SetExpr do Stmt;
for Element in CollectionExpr do Stmt;
for Element in Record do Stmt;
The type of the iteration variable Element must match the type held in the container. With each iteration of the loop, the iteration variable holds the current collection member. As with regular for-loops, the iteration variable must be declared within the same block as the for statement.
Warning: The iteration variable cannot be modified within the loop. This includes assignment, and passing the variable to a var parameter of a procedure. Doing so results in a compile-time warning.
Array expressions can be single or multidimensional, fixed length, or dynamic arrays. The array is traversed in increasing order, starting at the lowest array bound and ending at the array size minus one. The following code shows an example of traversing single, multi-dimensional, and dynamic arrays:
type
TIntArray = array[0..9] of Integer;
TGenericIntArray = array of Integer;
var
IArray1: array[0..9] of Integer = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
IArray2: array[1..10] of Integer = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
IArray3: array[1..2] of TIntArray = ((11, 12, 13, 14, 15, 16, 17, 18, 19, 20),
(21, 22, 23, 24, 25, 26, 27, 28, 29, 30));
MultiDimTemp: TIntArray;
IDynArray: TGenericIntArray;
I: Integer;
begin
for I in IArray1 do
begin
// Do something with I...
end;
// Indexing begins at lower array bound of 1.
for I in IArray2 do
begin
// Do something with I...
end;
// Iterating a multi-dimensional array
for MultiDimTemp in IArray3 do // Indexing from 1..2
for I in MultiDimTemp do // Indexing from 0..9
begin
// Do something with I...
end;
// Iterating over a dynamic array
IDynArray := IArray1;
for I in IDynArray do
begin
// Do something with I...
end;
The following example demonstrates iteration over string expressions:
var
C: Char;
S1, S2: String;
Counter: Integer;
OS1, OS2: ShortString;
AC: AnsiChar;
begin
S1 := 'Now is the time for all good men to come to the aid of their country.';
S2 := '';
for C in S1 do
S2 := S2 + C;
if S1 = S2 then
WriteLn('SUCCESS #1');
else
WriteLn('FAIL #1');
OS1 := 'When in the course of human events it becomes necessary to dissolve...';
OS2 := '';
for AC in OS1 do
OS2 := OS2 + AC;
if OS1 = OS2 then
WriteLn('SUCCESS #2');
else
WriteLn('FAIL #2');
end.
The following example demonstrates iteration over set expressions:
type
TMyThing = (one, two, three);
TMySet = set of TMyThing;
TCharSet = set of Char;
var
MySet: TMySet;
MyThing: TMyThing;
CharSet: TCharSet;
{$IF DEFINED(CLR)}
C: AnsiChar;
{$ELSE}
C: Char;
{$IFEND}
begin
MySet := [one, two, three];
for MyThing in MySet do
begin
// Do something with MyThing...
end;
CharSet := [#0..#255];
for C in CharSet do
begin
// Do something with C...
end;
end.
To use the for-in loop construct on a class or interface, the class or interface must implement a prescribed collection pattern. A type that implements the collection pattern must have the following attributes:
The class or interface must contain a public instance method called GetEnumerator(). The GetEnumerator() method must return a class, interface, or record type.
The class, interface, or record returned by GetEnumerator() must contain a public instance method called MoveNext(). The MoveNext() method must return a Boolean.
The class, interface, or record returned by GetEnumerator() must contain a public instance, read-only property called Current. The type of the Current property must be the type contained in the collection.
If the enumerator type returned by GetEnumerator() implements the IDisposable interface, the compiler will call the Dispose method of the type when the loop terminates.
The following code demonstrates iterating over an enumerable container in Delphi.
type
TMyIntArray = array of Integer;
TMyEnumerator = class
Values: TMyIntArray;
Index: Integer;
public
constructor Create;
function GetCurrent: Integer;
function MoveNext: Boolean;
property Current: Integer read GetCurrent;
end;
TMyContainer = class
public
function GetEnumerator: TMyEnumerator;
end;
constructor TMyEnumerator.Create;
begin
inherited Create;
Values := TMyIntArray.Create(100, 200, 300);
Index := -1;
end;
function TMyEnumerator.MoveNext: Boolean;
begin
if Index < High(Values) then
begin
Inc(Index);
Result := True;
end
else
Result := False;
end;
function TMyEnumerator.GetCurrent: Integer;
begin
Result := Values[Index];
end;
function TMyContainer.GetEnumerator: TMyEnumerator;
begin
Result := TMyEnumerator.Create;
end;
var
MyContainer: TMyContainer;
I: Integer;
Counter: Integer;
begin
MyContainer := TMyContainer.Create;
Counter := 0;
for I in MyContainer do
Inc(Counter, I);
WriteLn('Counter = ', Counter);
end.
The following classes and their descendents support the for-in syntax:
TList
TCollection
TStrings
TInterfaceList
TComponent
TMenuItem
TCustomActionList
TFields
TListItems
TTreeNodes
TToolBar
以上介绍了“ 你们好,我想问一下,vb 中的 For Each in 循环语句,delphi 中是那条语句呢?请帮助。”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2328246.html