CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Threaded View

  1. #1
    Join Date
    Oct 2006
    Posts
    181

    Enhancing Looping With Enumerators

    Before using enumerators I would have have an exposed data structures and loops like this:

    Code:
    ' 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.

    Code:
    ' 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
    Code:
    ' 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,
    Last edited by Scott.Macmaster; December 10th, 2008 at 04:45 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured