How to delete a photo from a blog?
Problem with Ienumerable<T>
Hello Guest
  
  • Login
• Register…
• Start blog
  • Who, Where, When
• What is interesting here?
• Duels
  • Polls
• Avatars
• Interests
  • Cities and Countries
• Random blog
• Users search
  • Search
• Games
• Tests
• QAIX
  • Сообщества
• Talxy Chat
• Horoscope
• Online
 
Register!

QAIX > .Net Development > Problem with Ienumerable<T> 24 June 2009 13:25:14

  Top users: 
  Recent blog posts: 
  Forums:   
  Discuss: 
  Recent forum topics: 
  Recent forum comments:
  Модератор:

Problem with Ienumerable<T>

Trey Nash 4 July 2005 02:52:45
 Can someone shed any light on why this code will not compile? The compiler
pitches a CS0305 error. This is very frustrating indeed. The problem seems
to be with the IEnumerable.GetEnum­erator() method and may be related to the
controversial decision my the Whidbey folks to derive IEnumerable<T> from
IEnumerable.

Thanks,

-Trey

-------------------­------------------
using System;
using System.Collections.­Generic;

public class MyContainer<T> : IEnumerable<T>
{
public void Add( T item ) {
impl.Add( item );
}

public void Add<R>( MyContainer<R> otherContainer ) {
foreach( item R in otherContainer ) {
impl.Add( item );
}
}

public IEnumerator<T> GetEnumerator() {
foreach( item T in impl ) {
yield return item;
}
}

IEnumerator IEnumerable.GetEnum­erator() {
return GetEnumerator();
}

private List<T> impl;
}

public class EntryPoint
{
static void Main() {
MyContainer<long> lContainer = new MyContainer<long>()­;
MyContainer<int> iContainer = new MyContainer<int>();­

lContainer.Add( 1 );
lContainer.Add( 2 );
iContainer.Add( 3 );
iContainer.Add( 4 );

lContainer.Add( iContainer );

foreach( long l in lContainer ) {
Console.WriteLine( l );
}
}
}

===================­================
This list is hosted by DevelopMentor http://www.develop.­com

View archives and manage your subscription(s) at http://discuss.deve­lop.com

Add comment
Shawn Wildermuth 4 July 2005 03:02:01 permanent link ]
 Once I qualify the GetEnumerator it works sort of:

System.Collections.­IEnumerator
System.Collections.­IEnumerable.GetEnume­rator()
{
return GetEnumerator();
}

It is then complaining about some errors which look like valid syntax
errors:

d:\Working\whidbey_­cs_sandbox\Class1.cs­(13,14): error CS0246: The type or
namespace name 'item' could not be found (are you missing a using directive
or an assembly reference?)
d:\Working\whidbey_­cs_sandbox\Class1.cs­(13,19): error CS0412: 'R': a
parameter or local variable cannot have the same name as a method type
parameter
d:\Working\whidbey_­cs_sandbox\Class1.cs­(15,16): error CS0103: The name
'item' does not exist in the current context
d:\Working\whidbey_­cs_sandbox\Class1.cs­(21,14): error CS0246: The type or
namespace name 'item' could not be found (are you missing a using directive
or an assembly reference?)
d:\Working\whidbey_­cs_sandbox\Class1.cs­(23,20): error CS0103: The name
'item' does not exist in the current context

Thanks,

Shawn Wildermuth
http://adoguy.com
C# MVP, MCSD.NET, Author and Speaker

->-----Original Message-----
->From: Unmoderated discussion of advanced .NET topics.
->[mailto:ADVANCED-DOTNET@DISCUSS.DEVELOP.COM] On Behalf Of Trey Nash
->Sent: Sunday, July 03, 2005 6:53 PM
->To: ADVANCED-DOTNET@DIS­CUSS.DEVELOP.COM
->Subject: [ADVANCED-DOTNET] Problem with Ienumerable<T>
->
->Can someone shed any light on why this code will not compile?
-> The compiler pitches a CS0305 error. This is very
->frustrating indeed. The problem seems to be with the
->IEnumerable.GetEn­umerator() method and may be related to the
->controversial decision my the Whidbey folks to derive
->IEnumerable<T> from IEnumerable.
->
->Thanks,
->
-> -Trey
->
->-----------------­--------------------­
->using System;
->using System.Collections.­Generic;
->
->public class MyContainer<T> : IEnumerable<T> {
-> public void Add( T item ) {
-> impl.Add( item );
-> }
->
-> public void Add<R>( MyContainer<R> otherContainer ) {
-> foreach( item R in otherContainer ) {
-> impl.Add( item );
-> }
-> }
->
-> public IEnumerator<T> GetEnumerator() {
-> foreach( item T in impl ) {
-> yield return item;
-> }
-> }
->
-> IEnumerator IEnumerable.GetEnum­erator() {
-> return GetEnumerator();
-> }
->
-> private List<T> impl;
->}
->
->public class EntryPoint
->{
-> static void Main() {
-> MyContainer<long> lContainer = new
->MyContainer<long>­();
-> MyContainer<int> iContainer = new MyContainer<int>();­
->
-> lContainer.Add( 1 );
-> lContainer.Add( 2 );
-> iContainer.Add( 3 );
-> iContainer.Add( 4 );
->
-> lContainer.Add( iContainer );
->
-> foreach( long l in lContainer ) {
-> Console.WriteLine( l );
-> }
-> }
->}
->
->=================­==================
->This list is hosted by DevelopMentor. http://www.develop.­com
->
->View archives and manage your subscription(s) at
->http://discuss.de­velop.com
07/03/2005 07:00:59 PM

===================­================
This list is hosted by DevelopMentor http://www.develop.­com

View archives and manage your subscription(s) at http://discuss.deve­lop.com

Add comment
Trey Nash 4 July 2005 04:22:49 permanent link ]
 SWEET! That did the trick. Now, if only I had thought to fully qualify
the beast. ;-)­

I'm still on the fence about deriving IEnumerable<T> from IEnumerable and
the same with IEnumerator. Sounds like someone needed a shortcut
somewhere. ;-)­

For good measure, I have included what the code should have looked like:

-------------------­----------------
using System;
using System.Collections.­Generic;

public class MyContainer<T> : IEnumerable<T>
{
public void Add( T item ) {
impl.Add( item );
}

public void Add<R>( MyContainer<R> otherContainer,
Converter<R, T> converter ) {
foreach( R item in otherContainer ) {
impl.Add( converter(item) );
}
}

public IEnumerator<T> GetEnumerator() {
foreach( T item in impl ) {
yield return item;
}
}

System.Collections.­IEnumerator
System.Collections.­IEnumerable.GetEnume­rator() {
return GetEnumerator();
}

private List<T> impl = new List<T>();
}

public class EntryPoint
{
static void Main() {
MyContainer<long> lContainer = new MyContainer<long>()­;
MyContainer<int> iContainer = new MyContainer<int>();­

lContainer.Add( 1 );
lContainer.Add( 2 );
iContainer.Add( 3 );
iContainer.Add( 4 );

lContainer.Add( iContainer,
EntryPoint.IntToLon­gConverter );

foreach( long l in lContainer ) {
Console.WriteLine( l );
}
}

static long IntToLongConverter(­ int i ) {
return i;
}
}
-------------------­----------------

Thanks,

-Trey
Once I qualify the GetEnumerator it works sort of:>
System.Collections.­IEnumerator> System.Collections.­IEnumerable.GetEnume­rator()> {> return GetEnumerator();> }>
It is then complaining about some errors which look like valid syntax> errors:>
d:\Working\whidbey_­cs_sandbox\Class1.cs­(13,14): error CS0246: The type or> namespace name 'item' could not be found (are you missing a using> directive> or an assembly reference?)> d:\Working\whidbey_­cs_sandbox\Class1.cs­(13,19): error CS0412: 'R': a> parameter or local variable cannot have the same name as a method type> parameter> d:\Working\whidbey_­cs_sandbox\Class1.cs­(15,16): error CS0103: The name> 'item' does not exist in the current context> d:\Working\whidbey_­cs_sandbox\Class1.cs­(21,14): error CS0246: The type or> namespace name 'item' could not be found (are you missing a using> directive> or an assembly reference?)> d:\Working\whidbey_­cs_sandbox\Class1.cs­(23,20): error CS0103: The name> 'item' does not exist in the current context>
Thanks,>
Shawn Wildermuth> http://adoguy.com> C# MVP, MCSD.NET, Author and Speaker>
->-----Original Message-----> ->From: Unmoderated discussion of advanced .NET topics.> ->[mailto:ADVANCED-DOTNET@DISCUSS.DEVELOP.COM] On Behalf Of Trey Nash> ->Sent: Sunday, July 03, 2005 6:53 PM> ->To: ADVANCED-DOTNET@DIS­CUSS.DEVELOP.COM> ->Subject: [ADVANCED-DOTNET] Problem with Ienumerable<T>> ->> ->Can someone shed any light on why this code will not compile?> -> The compiler pitches a CS0305 error. This is very> ->frustrating indeed. The problem seems to be with the> ->IEnumerable.GetEn­umerator() method and may be related to the> ->controversial decision my the Whidbey folks to derive> ->IEnumerable<T> from IEnumerable.> ->> ->Thanks,> ->> -> -Trey> ->> ->-----------------­--------------------­> ->using System;> ->using System.Collections.­Generic;> ->> ->public class MyContainer<T> : IEnumerable<T> {> -> public void Add( T item ) {> -> impl.Add( item );> -> }> ->> -> public void Add<R>( MyContainer<R> otherContainer ) {> -> foreach( item R in otherContainer ) {> -> impl.Add( item );> -> }> -> }> ->> -> public IEnumerator<T> GetEnumerator() {> -> foreach( item T in impl ) {> -> yield return item;> -> }> -> }> ->> -> IEnumerator IEnumerable.GetEnum­erator() {> -> return GetEnumerator();> -> }> ->> -> private List<T> impl;> ->}> ->> ->public class EntryPoint> ->{> -> static void Main() {> -> MyContainer<long> lContainer = new> ->MyContainer<long>­();> -> MyContainer<int> iContainer = new MyContainer<int>();­> ->> -> lContainer.Add( 1 );> -> lContainer.Add( 2 );> -> iContainer.Add( 3 );> -> iContainer.Add( 4 );> ->> -> lContainer.Add( iContainer );> ->> -> foreach( long l in lContainer ) {> -> Console.WriteLine( l );> -> }> -> }> ->}> ->> ->=================­==================> ->This list is hosted by DevelopMentor. http://www.develop.­com> ->> ->View archives and manage your subscription(s) at> ->http://discuss.de­velop.com> 07/03/2005 07:00:59 PM>
===================­================> This list is hosted by DevelopMentorВ® http://www.develop.­com>
View archives and manage your subscription(s) at> http://discuss.deve­lop.com>

===================­================
This list is hosted by DevelopMentorВ® http://www.develop.­com

View archives and manage your subscription(s) at http://discuss.deve­lop.com

Add comment
Guest 24 June 2009 13:25:14 permanent link ]
 :-P­ :-P­ :-P­ :-P­ :-P­ :-P­ :-P­ :-P­ :-P­ :-O­ :-O­ :-O­ :-\­ :-?­ ;~)­ (:|­ 8-}­ :-$­ 8-|­
Add comment
 

Add new comment

As:
Login:  Password:  
 
 
  
 
Пожалуйста, относитесь к собеседникам уважительно, не используйте нецензурные слова, не злоупотребляйте заглавными буквами, не публикуйте рекламу и объявления о купле/продаже, а также материалы нарушающие сетевой этикет или законы РФ. Ваш ip-адрес записывается.


QAIX > .Net Development > Problem with Ienumerable<T> 24 June 2009 13:25:14

see also:
[JBoss jBPM] - Re: Dates problem
[JBoss jBPM] - Re: Getting process…
[JBossCache] - Re: TreeCache…
pass tests:
You and sex.
see also:
How to enjoy DVDs on iPod/Zune/iPhone
How to convert AVCHD video
How to copy a DVD(on Mac)and burn a…

  Copyright © 2001—2010 QAIX
Идея: Монашёв Михаил.
Авторами текстов, изображений и видео, размещённых на этой странице, являются пользователи сайта.
See Help and FAQ in the community support.qaix.com.
Write in the community about the bugs you have noticedbugs.qaix.com.
Write your offers and comments in the communities suggest.qaix.com.
Information for parents.
Пишите нам на .
If you would like to report an abuse of our service, such as a spam message, please .
Если Вы хотите пожаловаться на содержимое этой страницы, пожалуйста .