foreach statement cannot operate on variables of type 에러 현상

 

using System.Collections.Generic;
using System.Collections;

using System.Linq;
using System.Text;
 

foreach (WATATMEGA128Sim.Lines ls in this.oLines)
{
}

 

위 형태로 코드가 작성되었을 때 아래와 같은 에러가 발생할때가 있습니다.

 

 

오류 1 foreach statement cannot operate on variables of type 'WATATMEGA128Sim.Lines' because 'WATATMEGA128Sim.Lines' does not contain a public definition for 'GetEnumerator' D:\data2\WAT_APP\WATATMEGA128Sim\WATATMEGA128Sim\WATATMEGA128Sim\Pin.cs 25 17 WATATMEGA128Sim

 

 

 


foreach 에서는 System.Collections.IEnumerable 를 사용하게 되어 있는데,

                     System.Collections.Generic.IEnumerable 는로 사용되어서 나타나는 현상이라고 합니다 ;

참고 주소 : http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/4c9b60fa-f2a5-4505-999e-a85c4d685071/

 

 

 

 

 

 

foreach statement cannot operate on variables of type 해결 방법 

 

이럴 땐 아래처럼 IEnumerable 형태로 변환한 후 사용하면 됩니다.

 

               System.Collections.IEnumerable scEnum = this.oLines as System.Collections.IEnumerable;

                foreach (WATATMEGA128Sim.Lines ls in scEnum)
                {
                }

 

 

 

+ Recent posts