enumerator in custom configuration section

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • captainB
    New Member
    • Aug 2009
    • 23

    enumerator in custom configuration section

    greetings!
    In my asp.net application, I created a custom section in web.config that contain strings that are quotes to display in my home page. Every request for my home page (default.aspx) grabs a quote based on a random index.

    my web.config section looks like this:
    <configSections >
    ...
    <section name="quotesSec tion" type="QuotesSec tion"/>
    </configSections>

    <quotesSectio n>
    <quotesCollecti on>
    <add value=" quote text here"/>
    .......
    </quotesCollectio n>
    </quotesSection>

    in my quotesHandler class I have the following:

    public class QuotesCollectio n : ConfigurationEl ementCollection , IEnumerable
    {
    .....
    #region IEnumerable Members

    IEnumerator IEnumerable.Get Enumerator()
    {
    return (IEnumerator)ne w QuotesEnumerato r(this);
    }

    #endregion
    }

    The region above was created by visual studio (when I clicked the option to "Explicitly Implement interface 'IEnumerable' " after hovering over the IEnumerable keyword).

    I have a static class "myQuotes" that populates a List<string> object like so:

    public static class myQuotes
    {
    static public List<string> quotes = new List<string>();

    staticmyQuotes( )
    {

    QuotesSection _quotes = (QuotesSection) ConfigurationMa nager.GetSectio n("quotesSectio n");
    foreach (Quote quote in _quotes.quotes)
    {
    quotes.Add(quot e.Value);
    }

    }

    This is a learning experiment in implementing interfaces, specifically IEnumerable and IEnumerator.
    So this works fine, but when I set a break point at the GetEnumerator() method in the QuotesCollectio n class (which implements IEnumerable), it never stops there, the foreach loop just works. I am not implementing any of the IEnumerator methods (Current(), Reset(), MoveNext()...).

    I was able to code my own GetEnumerator() method and return my own IEnumerator class which implemented the required methods.
    Is that overriding the base class (ConfigurationE lementCollectio n) implementation of IEnumerator? Meaning - the GetEnumerator method is never called from my code?


    Thanks in advance.
Working...