How To Implement IEnumerable 1
Print Hello World
Write Basic Syntax
Use Properties
Make An If Else Ladder
Make A Branch Statement
Declare Inits
Use Lists
Use Arrays
Make A Class Hierarchy
Use Nil And Nilable Types
Use Dynamic Typing
Declare Variable Number Of Args
Read And Write Files
Check Inheritance And Implementation
Customize Object Equality
Pass References To Methods
Translate Pseudo Code To Cobra 1
Translate Pseudo Code To Cobra 2
Implement IEnumerable 1
Implement IEnumerable 2
Iterate Through Recursive Data With Yield
Make A Collection Class
Declare Contracts
Threads
Win Forms
WPF
GTK
Qyoto
Access MySQL
XNA
Open TK
 
"""
ImplementIEnumerable1.cobra

Implementing the IEnumerable interface allows an object to be used in a `for`
loop such as:

    for record in fileCabinet
        print record

and to be passed wherever an IEnumerable is expected such as the List
initializer:

    records = List<of Record>(fileCabinet)


See also: ImplementIEnumerable1.cobra
"""


class FileCabinet implements IEnumerable<of Record>

    var _records = List<of Record>()

    def newRecord(name as String) as Record
        r = Record(_records.count+1, name)
        _records.add(r)
        return r

    def getEnumerator as IEnumerator<of Record>
        return _records.getEnumerator

    def getEnumerator as System.Collections.IEnumerator
        implements System.Collections.IEnumerable
        return .getEnumerator


class Record

    cue init(serialNum as int, name as String)
        base.init
        _serialNum, _name = serialNum, name

    pro serialNum from var as int

    pro name from var as String

    def toString as String is override
        return '[.typeOf.name]([_serialNum], [_name])'


class Program

    def main
        # build a cabinet:
        cabinet = FileCabinet()
        cabinet.newRecord('Red')
        cabinet.newRecord('Blue')
        cabinet.newRecord('Green')

        # because cabinet is an IEnumerable, it can be for'ed:
        for record in cabinet
            print record

        # and passed to a List initializer:
        records = List<of Record>(cabinet)
        for record in records
            assert record.serialNum > 0