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="quotesSection" type="QuotesSection"/>
</configSections>
<quotesSection>
<quotesCollection>
<add value=" quote text here"/>
.......
</quotesCollection>
</quotesSection>
in my quotesHandler class I have the following:
public class QuotesCollection : ConfigurationElementCollection, IEnumerable
{
.....
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)new QuotesEnumerator(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)ConfigurationManager.GetSection("qu otesSection");
foreach (Quote quote in _quotes.quotes)
{
quotes.Add(quote.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 QuotesCollection 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 (ConfigurationElementCollection) implementation of IEnumerator? Meaning - the GetEnumerator method is never called from my code?
Thanks in advance.