Wiki

root/cobra/trunk/HowTo/350-MakeACollectionClass.cobra

Revision 2335, 1.2 KB (checked in by Chuck.Esterbrook, 2 years ago)

Improvements to the How-To's.

  • Property svn:eol-style set to native
Line 
1"""
2MakeACollectionClass.cobra
3
4This HowTo shows the syntax and techniques for declaring a class that
5implements IList<of T>.
6"""
7
8
9class MyList<of T> implements IList<of T>
10
11    var _items as List<of T>
12
13    cue init
14        base.init
15        _items = List<of T>()
16
17    ## IEnumerable ##
18
19    def getEnumerator as IEnumerator<of T>
20        return _items.getEnumerator
21
22    def getEnumerator as System.Collections.IEnumerator
23        implements System.Collections.IEnumerable
24        return .getEnumerator
25
26
27    ## ICollection ##
28
29    get count as int
30        return _items.count
31
32    get isReadOnly as bool
33        return false
34
35    def add(item as T)
36        _items.add(item)
37
38    def clear
39        _items.clear
40
41    def contains(item as T) as bool
42        return _items.contains(item)
43
44    def remove(item as T) as bool
45        return _items.remove(item)
46
47    def copyTo(array as T[], index as int)
48        _items.copyTo(array, index)
49
50
51    ## IList ##
52
53    pro [index as int] as T
54        get
55            return _items[index]
56        set
57            _items[index] = value
58
59    def indexOf(x as T) as int
60        return _items.indexOf(x)
61
62    def insert(index as int, x as T)
63        _items.insert(index, x)
64
65    def removeAt(index as int)
66        _items.removeAt(index)
67
68
69class Program
70
71    def main
72        t = MyList<of int>()
73        t.add(5)
74        assert t.count==1
Note: See TracBrowser for help on using the browser.