Implementing C#'s foreach loop in Delphi 8

CK CK 3788 
Created at  
464 0 0
In C# the foreach statement repeats a group of embedded statements for each element in an array or an object collection. Delphi 8 does not have an eqivalent of the foreach statement. Let's see how to code a foreach statement in Delphi...

First, here's a simple sample C# code:

foreach (TMyObject myObject in AnArrayList) {
  AnIntValue = MyObject.MyValue;
}
Where TMyObject class is declared as:
TMyObject = Class(TObject)
  public
    MyField : integer;
    constructor Create(AValue:integer) ;
end;

constructor TMyObject.Create(AValue: integer) ;
begin
   inherited Create;
   MyField := AValue;
end;


Here's the "same code" in Delphi:

var
   MyObject : TMyObject;
   AnArrayList : ArrayList;
   Enum: IEnumerator;
   j:integer;
begin
   AnArrayList := ArrayList.Create;
   AnArrayList.Add(TMyObject.Create(2004)) ;
   AnArrayList.Add(TMyObject.Create(1973)) ;
   AnArrayList.Add(TMyObject.Create(2000)) ;
   AnArrayList.Add(TMyObject.Create(1998)) ;


   //FOR EACH
   Enum := AnArrayList.GetEnumerator;
   while Enum.MoveNext do
   begin
     MyObject := TMyObject(Enum.Current) ;
     Response.Write(MyObject.MyField.ToString + '<br>') ;
   end;

   // OR USING ITERATION
   for j:= 0 to -1 + AnArrayList.Count do
   begin
     MyObject := TMyObject(AnArrayList[j]) ;
     Response.Write(MyObject.MyField.ToString + '<br>') ;
   end;
Tags Delphi create a thumbnail email Facebook X
Comments 0
Similar posts
  1. Sending email messages in .Net
    1,041
  2. Delphi for .Net Code Folding keyboard shortcuts
    531
  3. How to encrypt a string
    469
  4. Creating thumbnail images
    867
  5. Technology sharing related with software development. Specially content will be focused to programming language, and embedded systems.
    856
  6. Rotate Bitmap (any angle, any center of rotation)
    409
  7. Why SPF and DKIM Are Essential for Linux Email Servers?
    51
  1. How to set an certain color as transparent color on bitmap
    526
  2. How to resize and save PNG or Jpeg Image file ?
    638
  3. How to display message box in C#
    1,129
  4. How to convert bitmap image to jpeg image ?
    362
  5. To load image from file
    457
  6. URL Encode, Decode function for MFC
    609
  7. How to load HTML resource on MFC ?
    848
  8. How to get GM Time as String ?
    424
  9. Makefile to automate the build process for C++
    285
  10. Finding process name on OS
    414
  11. Convert System.DateTime to UNIX timestamp
    386
  12. Retrieve system information
    498
  13. How to call SetTimer function on MFC CDialog class ?
    652
  14. How to rename(change file name) in C# ?
    1,086
  15. Find Files in certain directory
    937
  16. How to detect resource leaks in a Form
    950
  17. The best way to enable and disable buttons, menu items and toolbar buttons
    668
  18. How to exit from a console application
    816
  19. How to restrict a program to a single instance
    733
  20. Compress and decompress strings in C#
    977
  21. Graphics in C# - Irregular Forms
    914
  22. Extracting the Country from IP Address
    471
  23. How to run shell command by MFC ?
    441
  24. Binary Search Sample Code
    409
  25. Double Linked List based in C++
    800
  26. String Replace Function For C#
    1,561
  27. How to print character in C# by ASCII code
    480
  28. MFC based World Wide Web HTTP Server Source Code
    463
  29. Creating and Using a CAsyncSocket Object to use CAsyncSocket
    473
  30. UDP Send and Receive Using CAsyncSocket
    1,207
Recently updated
  1. Google OAuth Token Expiration: Causes and Solutions
    31
  2. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    57
  3. How do I create animated background for Google Meet?
    23,256
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    273
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    105
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    170
  7. Array of arguments passed to script in PHP
    99
  8. Invalid Auction Storms in Ad Tech
    108
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    209
  10. The Two Clocks of Ad Server Architecture
    78
  11. Frequency Cap Architecture Overview
    527
  12. Collecting Proof-of-Play from 100,000 Screens
    154
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    303
  14. What is RedisVL?
    117
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    247
  16. TPU & GPU Cluster Management Tools
    258
  17. The Concept of Scaffolding in AI Agents
    245
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    599
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    269
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    355
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    207
  22. Machine Learning: Training vs. Ranking
    231
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    229
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    204
  25. Designing a Data Curation System Around a Vector Database
    230
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    263
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    674
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    201
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    233
  30. Understanding Data Lineage
    272