Scott.Macmaster
December 10th, 2008, 03:42 PM
Before using enumerators I would have have an exposed data structures and loops like this:
' Exposed data structures
Public Blocks As New SortedList(Of Integer, Block)
Public RedBlocks As New SortedList(Of Integer, Block)
Public BlueBlocks As New SortedList(Of Integer, Block)
' Loops
For Each blockKVP As KeyValuePair(Of Integer, Block) In Blocks
Dim block As Block = blockKVP.Value
...
Next
For Each blockKVP As KeyValuePair(Of Integer, Block) In BlueBlocks
Dim block As Block = blockKVP.Value
...
Next
Whenever code added/removed blocks to the list the code had to make sure to add/remove from each list. This of course is error prone. Also, if I wanted to maintain another list of blocks in a different color I'd have to modify each block of code that added/removed from the lists.
So I created enumerator classes to encapsulate all this and to protect the data structures. It also made the loop syntax simpler and easier to read.
' Enumerators
Public Class BlockEnumerator
Implements IEnumerator(Of Block)
...
End Class
Public Class RedBlockEnumator
Implements IEnumerator(Of Block)
....
End Class
Public Class BlockList
Implements IEnumerable(Of Block)
...
End Class
Public Class RedBlockList
Implements IEnumerable(Of Block)
...
End Class
' Protected data structures
Private _Blocks As New SortedList(of integer, Block)
Public Blocks As New BlockList(_Blocks)
Public RedBlocks As New BlockList(_Blocks)
' Simple loops
For Each block In Blocks
...
Next
For Each block In RedBlocks
...
Next
Now my question. I would like to only have one public list and to select the subset of blocks in the looping syntax. I have few thoughts what the syntax might be but I don't know what class/data structures I need in order to do this.
Code Examples
' Protected data structures
Private _Blocks As New SortedList(of integer, Block)
Public Blocks As New BlockList(_Blocks)
' Loops through all blocks
For Each block In Blocks
...
Next
For Each block In Blocks.AllBlocks
...
Next
For Each block In Blocks(BlockColor.AllColors)
...
Next
' Loops through red blocks
For Each block In Blocks.RedBlocks
...
Next
For Each block In Blocks(BlockColor.RedBlocks)
...
Next
Well, does anyone have any suggestions or know if this is even possible?
Thanks,
' Exposed data structures
Public Blocks As New SortedList(Of Integer, Block)
Public RedBlocks As New SortedList(Of Integer, Block)
Public BlueBlocks As New SortedList(Of Integer, Block)
' Loops
For Each blockKVP As KeyValuePair(Of Integer, Block) In Blocks
Dim block As Block = blockKVP.Value
...
Next
For Each blockKVP As KeyValuePair(Of Integer, Block) In BlueBlocks
Dim block As Block = blockKVP.Value
...
Next
Whenever code added/removed blocks to the list the code had to make sure to add/remove from each list. This of course is error prone. Also, if I wanted to maintain another list of blocks in a different color I'd have to modify each block of code that added/removed from the lists.
So I created enumerator classes to encapsulate all this and to protect the data structures. It also made the loop syntax simpler and easier to read.
' Enumerators
Public Class BlockEnumerator
Implements IEnumerator(Of Block)
...
End Class
Public Class RedBlockEnumator
Implements IEnumerator(Of Block)
....
End Class
Public Class BlockList
Implements IEnumerable(Of Block)
...
End Class
Public Class RedBlockList
Implements IEnumerable(Of Block)
...
End Class
' Protected data structures
Private _Blocks As New SortedList(of integer, Block)
Public Blocks As New BlockList(_Blocks)
Public RedBlocks As New BlockList(_Blocks)
' Simple loops
For Each block In Blocks
...
Next
For Each block In RedBlocks
...
Next
Now my question. I would like to only have one public list and to select the subset of blocks in the looping syntax. I have few thoughts what the syntax might be but I don't know what class/data structures I need in order to do this.
Code Examples
' Protected data structures
Private _Blocks As New SortedList(of integer, Block)
Public Blocks As New BlockList(_Blocks)
' Loops through all blocks
For Each block In Blocks
...
Next
For Each block In Blocks.AllBlocks
...
Next
For Each block In Blocks(BlockColor.AllColors)
...
Next
' Loops through red blocks
For Each block In Blocks.RedBlocks
...
Next
For Each block In Blocks(BlockColor.RedBlocks)
...
Next
Well, does anyone have any suggestions or know if this is even possible?
Thanks,