3 Replies - 1278 Views - Last Post: 30 May 2011 - 01:14 AM Rate Topic: -----

#1 thesamy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 12-July 10

IEnumerable and IEnumerator problem

Posted 29 May 2011 - 04:07 AM

im learing to enabled the foreach loop in arry i creat with IEnmorable and IEnumrator interfaces
and i got a problem that i cant understand why it happning
when i first use this interface with int arry every thing work great but now im trying to do it with
arry of doubles that foreach loop work but it dosnt show the Current as i want it to show

i want the current return the temperatur in fernhait but its just stay the same
(sorry for my english)


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Temperature tem = new Temperature();
            tem.temps = new double[] { 2.3, 4.5, 6.4, 6.7, 8.7 };
            foreach (var i in tem.temps)
            {
                Console.WriteLine(i);
            }
        }
    }
    class Temperature : IEnumerable
    {
        public double[] temps { get; set; }
        public IEnumerator GetEnumerator()
        {
            return new TempertatureIEnomerator { other = this };
        }

        class TempertatureIEnomerator : IEnumerator
        {
            public Temperature other {get;set;}
            int size = -1;
            public object Current
            {
                get 
                {
                    other.temps[size] = (other.temps[size] - 32) * (5 / 9);
                    return other.temps[size];
                    
                }
            }

            public bool MoveNext()
            {
                size++;
                if (size < other.temps.Length)
                    return true;
                return false;
            }

            public void Reset()
            {
                return;
            }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: IEnumerable and IEnumerator problem

#2 thesamy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 12-July 10

Re: IEnumerable and IEnumerator problem

Posted 29 May 2011 - 05:12 AM

amm i got it to work but only with genrics can somone expline me why this didnt work with the older code

this is the new code:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Temperatur temp = new Temperatur { Temps = new double[]{ 23.5, 43.3, 32.5, 34.5 }};
            foreach (double i in temp.Temps)
            {
                Console.WriteLine(i);
            }
           
        }

    }
    class Temperatur : IEnumerable<double>
    {
        public double[] Temps { get; set; }


        public IEnumerator<double> GetEnumerator()
        {
            return new TemperaturIEnumrator { Frenhit = Temps };
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        //inner class
        class TemperaturIEnumrator : IEnumerator<double>
        {
            public double[] Frenhit { get; set; }
            int size = -1;
            public double Current
            {
                get { return Frenhit[size] * 9 / 32 + 5; }
            }

            public void Dispose()
            {
                return;
            }

            object System.Collections.IEnumerator.Current
            {
                get { throw new NotImplementedException(); }
            }

            public bool MoveNext()
            {
                size++;
                return (size < Frenhit.Length);
            }

            public void Reset()
            {
                return;
            }
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#3 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: IEnumerable and IEnumerator problem

Posted 29 May 2011 - 05:58 AM

Hi,

The first, non generic version didn't work because you weren't using your Temperature IEnumerable object in your foreach loop. You were looping through the double array (which is also an IEnumerable).

The whole point of implementing IEnumerable in this case is to allow you to use your Temperature object as if it were a collection.

For example, your doing this:

 foreach (var i in tem.temps)
 {  
         Console.WriteLine(i);
 }



Here, GetEnumerator() is called on tem.temps, which is a double array. Therefore, the IEnumerator object for the double array is being returned and used in the looping, NOT your IEnumerator object.


However, if you do this:

foreach (var i in tem)
 {  
         Console.WriteLine(i);
 }



Notice how I am no longer looping through the double array directly, but I am looping through your Temperature object.

Now, GetEnumerator() will be called on your Temperature object rather than on the double array, and GetEnumerator() will now return your custom IEnumerator (TemperaturIEnumrator) rather than the double array's IEnumerator, so your code should work.


...Although, this calculation:

(other.temps[size] - 32) * (5 / 9);


will always return 0 thanks to integer division on the 5 / 9 always resulting in 0. Change it to this:

(other.temps[size] - 32) * (5.0 / 9.0);



Any object that implements IEnumerable can be used in a for each loop. GetEnumerator() is called on you IEnumerable object before looping starts to obtain the IEnumerator object used for looping. The problem with your code ws that you were calling GetEnumerator() on the wrong IEnumerable object, and so were getting the wrong IEnumerator back!

The second, generic version worked simply because you hardcoded the Fahrenheit values into your double array. It still not really 'working' though as your custom enumerator isn't being used still :)

This post has been edited by CodingSup3rnatur@l-360: 29 May 2011 - 06:38 AM

Was This Post Helpful? 4
  • +
  • -

#4 thesamy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 12-July 10

Re: IEnumerable and IEnumerator problem

Posted 30 May 2011 - 01:14 AM

thank u very much from this, now i understand.
:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1