1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
// both Capability and OperationMode are held in the List<Group> this.capabilities
// useEnumerate.DoWrite shows how th build a generic call to handle subsets of this.capabilities.
// It offers an elegant and very readable call because the Enumerator is a parameter of the method
using System.Collections;
using System.Collections.Generic;
namespace Sniplet
{
#region Help Classes
// example base class
public class Group
{
public bool Included
{
get
{
return true;
}
}
}
// example Capability
public class Capability : Group
{
public Capability()
:base()
{
}
}
// Example OperationMode
public class OperationMode : Group
{
public OperationMode()
: base()
{
}
}
#endregion;
public class useEnumerate
{
private List<Group> capabilities;
public useEnumerate()
{
this.capabilities = new List<Group>();
// todo: fill this.capabilities
}
/// <summary>
/// since the enumerator is a part of the method this call automatically filters on this.capabilities
/// It enables you to handle each subset differently, for instance by providing a different tagName
/// </summary>
/// <param name="enm">The filter that will provide you with a subset of this.capabilities </param>
/// <param name="tagName">example of an additional parameter for the call</param>
/// <returns></returns>
private void DoWrite(IEnumerable enm, string tagName)
{
foreach (Group g in enm)
{
// Do something on the chosen subset of capabilities
// e.g.
// DoWriteTag(tagname);
}
}
/// <summary>
/// Example of a call to DoWrite
/// </summary>
/// <returns></returns>
protected void WriteTagNames()
{
DoWrite(this.ContainedCapabilities, "capability");
DoWrite(this.ContainedOperationModes, "mode");
}
#region Iterators
// enumerate Capabilities
internal IEnumerable<Capability> ContainedCapabilities
{
get
{
for (int i = 0; i < this.capabilities.Count; i++)
{
if ((this.capabilities[i] is Capability) && (this.capabilities[i].Included == true))
{
yield return this.capabilities[i] as Capability;
}
}
}
}
// enumerate modes
internal IEnumerable<OperationMode> ContainedOperationModes
{
get
{
for (int i = 0; i < this.capabilities.Count; i++)
{
if ((this.capabilities[i] is OperationMode) && (this.capabilities[i].Included == true))
{
yield return this.capabilities[i] as OperationMode;
}
}
}
}
#endregion;
}
}
|