skip to Main Content

I have created a custom object so that I can save/load data from my Unity game into a Firebase Database. The custom object Result consists of a list of tuples List<int,int,int)>.

public class Result
{
    public List<(int,int,int)> results;

    public Result(List<(int,int,int)> results) {
        this.results = results;
    }

    // get a list of one of the tuple items
    public List<int> getList(int id) {
        List<int> singlResults = new List<int>();
        // loop through each tuple
        foreach (var item in this) { //error here
        // do something
        }
    }
}

However when I try to loop over the List in the Result object I get an error:

foreach statement cannot operate on variables of type ‘Result’ because ‘Result’ does not contain a public instance definition for ‘GetEnumerator’

3

Answers


  1. Your Result class must implement IEnumerable and IEnumerator to iterate using the foreach loop.

    here you will find the details.

    Login or Signup to reply.
  2. Based on the your code, the error seems to be occurring because the Result class is not implementing the IEnumerable interface (which is required for the foreach loop to work) OR doesn’t contain a method called GetEnumerator() that the interface requires.

    The IEnumerable interface in C# provides the basic functionality required to allow an object to be enumerated using a foreach loop.

    You would want your code to look something like this:

    public class Result : IEnumerable<(int, int, int)>
    {
        public List<(int,int,int)> results;
    
        public Result(List<(int,int,int)> results) {
            this.results = results;
        }
    
        // get a list of one of the tuple items
        public List<int> getList(int id) {
            List<int> singlResults = new List<int>();
            // loop through each tuple
            foreach (var item in this) {
                // do something
            }
    
            return singlResults;
        }
    
        // implementation of the IEnumerable interface
        // needed to stop: does not contain a public instance definition for 'GetEnumerator'
        public IEnumerator<(int, int, int)> GetEnumerator() {
            return results.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator() {
            return GetEnumerator();
        }
    }
    
    Login or Signup to reply.
  3. Your Result itself actually doesn’t and doesn’t need to implement IEnumerable at all as suggested by others. That’s overcomplicating it a bit imho

    All you need to do is instead of trying to iterate this rather iterate the results and do

    public List<int> getList(int id)
    {
        var singlResults = new List<int>(results.Count);
    
        // loop through each tuple
        foreach (var item in results)
        {
            var value = id switch
            {
                0 => item.Item1,
                1 => item.Item2,
                2 => item.Item3,
                _ => throw new ArgumentException("Id can only be 0, 1 or 2!")
            };
    
            singleResults.Add(value);
        }
    
        return singleResults;
    }
    

    Could also go through Linq if you want and do

    using System.Linq;
    
    ...
    
    public List<int> getList(int id)
    {
        return results
            .Select(item => id switch
            {
                0 => item.Item1,
                1 => item.Item2,
                2 => item.Item3,
                _ => throw new ArgumentException("Id can only be 0, 1 or 2!")
            })
           .ToList();     
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search