Search notes:

Implementing IEnumerator in PowerShell to use an object in a foreach statement

An object is required to implement the System.Collections.IEnumerator interface in order for the foreach statement to be able to iterate over the object.
The following example tries to demonstrate how an object can implement this interface.
class tq84Enumerator: System.Collections.IEnumerator {

   [int] $val

   tq84Enumerator() {
       $this.Reset()
   }

   [object] get_Current() {
      return $this.val
   }

   [bool] MoveNext() {
      $this.val++

      if ($this.val -gt 13) {
         return $false
      }
      return $true
   }

   [void] Reset() {
       $this.val = 7
   }
}

$e = [tq84Enumerator]::new()
foreach ($num in $e) {
   write-host "num = $num"
}
Github repository about-powershell, path: /language/statement/foreach/IEnumerator.ps1

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Windows/...', 1758203023, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Windows/PowerShell/language/statement/foreach/IEnumerator(68): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78