Operator Step (Iteration)
Increments the iterator of a `For...Next` loop
Syntax
`
{ Type | Class | Union } typename
Declare Operator Step ()
Declare Operator Step ( [ ByRef | ByVal ] stp As typename )
...
End { Type | Class | Union }
`
Usage
`
For iterator [ As typename ] = start_value To end_value [ Step step_value ]
[ ...statements... ]
Next
`
Parameters
`typename`
name of the `Type`, `Class`, or `Union`
`stp`, `step_value`
a `typename` object used as an incremental value
`iterator`
a `typename` object used as an iterator
`end_value`
a `typename` object used as a loop-terminating value
`start_value`
a `typename` object used to copy construct or assign to the iterator initially
Description
`Operator For`, `Operator Next` and `Operator Step` can be overloaded in user-defined type definitions to allow objects of that type to be used as iterators and step values in `For...Next` loops.
`Operator Step` is called to increment the iterator immediately after all statements in the `For...Next` body are executed, if any.
The first version of `Operator Step` is used if no step value is given in the `For...Next` statement. If a step value is given, the second version is used and is passed the step value.
Example
`
'' Example Type
Type T
'' value is set by the constructor
value As Double
Declare Constructor( ByVal x As Double = 0 )
Declare Operator For( ByRef stp As T )
Declare Operator Step( ByRef stp As T )
Declare Operator Next( ByRef cond As T, ByRef stp As T ) As Integer
End Type
Constructor T ( ByVal x As Double )
Print "T iterator constructed with value " & x
value = x
End Constructor
Operator T.for( ByRef stp As T )
End Operator
Operator T.step( ByRef stp As T )
Print " incremented by " & stp.value & " in step."
value += stp.value
End Operator
Operator T.next( ByRef cond As T, ByRef stp As T ) As Integer
'' iterator's moving from a high value to a low value (step >= 0)
If( stp.value < 0 ) Then
Return( value >= cond.value )
Else
'' iterator's moving from a low value to a high value (step < 0)
Return( value <= cond.value )
End If
End Operator
'' Example Usage. It looks like we are working with numbers, but the iterators
'' have overloaded constructors. The 10, 1, and -1 are all of type T.
For i As T = 10 To 1 Step -1
Print i.value;
Next i
`
A more practical example demonstrating file iteration based on cha0s' file iteration class.
`
'' a class which iterates through files
Type FileIter
As String pathName, fileName
Declare Constructor( ByRef pathName As String )
Declare Operator For()
Declare Operator Step()
Declare Operator Next( ByRef endCond As FileIter) As Integer
End Type
Constructor FileIter( ByRef pathName As String )
this.pathName = pathName
End Constructor
Operator FileIter.for( )
fileName = Dir(pathName & "/*.*")
End Operator
Operator FileIter.step( )
fileName = Dir("")
End Operator
Operator FileIter.next( ByRef endCond As FileIter ) As Integer
Return(fileName <> endCond.pathName)
'' the c'tor sets the path name and so we check against that
End Operator
'' example code
'' change it to any directory
For i As FileIter = "./" To ""
Print i.fileName
Next
`
Dialect Differences

- Only available in the -lang fb dialect.

See also

- `Operator For`
- `Operator Next`
- `For...Next`