0

I have implemented a custom linked list and I am having trouble implementing the IEnumerator<>. Specifically, the compiler tells me The name "GetEnumerator" does not exist in the current context. I feel like I'm implementing it exactly how I have seen in numerous stackoverflow posts and tutorials, what am I missing?

Here is my data structure:

namespace TestReportCreator_v3
{
    public class FindingsTable : IEnumerable<string>
    {
        private Node head, mark;
        public class Node
        {
            public string description; //covers weakness, retinaDesc, nessusDesc
            public string riskLevel; //covers impactLevel, retinaRisk, nessusRisk
            public string boxName; //box name results apply to
            public string scanner; //wassp, secscn, retina, nessus
            public string controlNumber; //ia control number impacted, wassp and secscn only
            public string fixAction; //comments, retinaFix, nessusSolu
            public string auditID; //nessus plugin, retina AuditID, wassp/secscn test number
            public Node next;
            public Node(string auditID, string riskLevel, string boxName, string scanner, string controlNumber, string fixAction, string description, Node next)
            {
                this.description = description;
                this.riskLevel = riskLevel;
                this.boxName = boxName;
                this.scanner = scanner;
                this.controlNumber = controlNumber;
                this.fixAction = fixAction;
                this.auditID = auditID;
                this.next = next;
            }
        }

    ...insert, delete, update, save methods...

        public IEnumerator<string> IEnumerable<string>.GetEnumerator()
        {
            var node = mark;

            while (node != null)
            {
                yield return node.riskLevel;
                node = node.next;
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}
anaximander
  • 7,083
  • 3
  • 44
  • 62
Chris
  • 934
  • 1
  • 17
  • 38
  • 3
    Side note: it is strange that compiler did not complain about `public` in front of explicit interface implementation: `public IEnumerator IEnumerable.GetEnumerator()` – Alexei Levenkov Feb 06 '15 at 16:14
  • After adding the information from the answer below, it did scream about the public. Good call. Thank you. – Chris Feb 06 '15 at 17:06

1 Answers1

6

IEnumerable<string>.GetEnumerator() is an explicit interface implementation, so you need to call it on the instance, accessed as an interface. The easiest way to do this is to cast this:

return ((IEnumerable<string>)this).GetEnumerator();

See How to call explicit interface implementation methods internally without explicit casting? for alternatives.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272