F
Flinchvoid
Interesting little problem; go easy on me because I'm just a humble
scripter turned c# developer.
I've a class UserList which I auto-generated with a python script, it
extends CollectionBase and has the usual methods: this[int index],
Add, Insert, Remove and Contains. There are no differences between
this class and the other generated classes I'm using other than the
type of the contained object. The other classes can be foreach'ed
quite happily.
I knocked up a quick console app to test an aspect of this system. I
can iterate the members of the list with an integer index, but not
with a foreach. The method called inside the loop, GetName, does not
alter the underlying object, it just returns a formatted string. Can
anybody shed any light on this peculiar quirk?
// this works
UserList l = dataprovider.GetLocalUsers();
int c = l.Count;
while(c-- > 0){
User u = l[c];
Console.WriteLine(u.GetName(User.NameFormat.InitialSurname));
}
// this causes a compiler error
// foreach(User u in l)
// {
// Console.WriteLine(u.GetName(u.NameFormat.InitialSurname));
// }
scripter turned c# developer.
I've a class UserList which I auto-generated with a python script, it
extends CollectionBase and has the usual methods: this[int index],
Add, Insert, Remove and Contains. There are no differences between
this class and the other generated classes I'm using other than the
type of the contained object. The other classes can be foreach'ed
quite happily.
I knocked up a quick console app to test an aspect of this system. I
can iterate the members of the list with an integer index, but not
with a foreach. The method called inside the loop, GetName, does not
alter the underlying object, it just returns a formatted string. Can
anybody shed any light on this peculiar quirk?
// this works
UserList l = dataprovider.GetLocalUsers();
int c = l.Count;
while(c-- > 0){
User u = l[c];
Console.WriteLine(u.GetName(User.NameFormat.InitialSurname));
}
// this causes a compiler error
// foreach(User u in l)
// {
// Console.WriteLine(u.GetName(u.NameFormat.InitialSurname));
// }