编译错误与foreach? [英] Compiler error with foreach?

查看:85
本文介绍了编译错误与foreach?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有趣的小问题;对我很轻松,因为我只是一个谦虚的

脚本编写者转向c#开发者。


我有一个类自动生成的UserList一个python脚本,它是
扩展CollectionBase并具有通常的方法:这[int index],

添加,插入,删除和包含。

这个类与其他生成的类之间没有区别,我使用的是除了包含对象的

类型之外的其他类。其他课程可以非常愉快地使用




我敲了一个快速的控制台应用来测试这个系统的一个方面。我用
可以用整数索引迭代列表成员,但不能用foreach迭代
。在循环内部调用GetName的方法不会改变底层对象,它只返回一个格式化的字符串。可以

任何人对这个奇怪的怪癖有所了解吗?


//这个有用吗


UserList l = dataprovider .GetLocalUsers();


int c = l.Count;


while(c--> 0){

用户u = l [c];

Console.WriteLine(u.GetName(User.NameFormat.Initia lSurname));

}

//这导致编译器错误


// foreach(用户u in l)

// {

// Console.WriteLine(u.GetName(u.NameFormat.InitialSu rname));

//}

解决方案

< blockquote> Flinchvoid写道:

有趣的小问题;对我很轻松,因为我只是一个谦虚的编码开发人员。



< snip>


你的用户列表集合类实现了IEnumerable吗?


-

Lasse V?gs?ther Karlsen
http://www.vkarlsen.no/

mailto:la *** @ vkarlsen.no

PGP KeyID:0x0270466B


扩展CollectionBase是不够的 - 你需要实现

GetEnumerator - 参见MSDN on foreach


Flinchvoid写道:

有趣的小问题;对我很轻松,因为我只是一个谦虚的脚本编写者转向c#开发者。

我有一个类UserList,我用python脚本自动生成,它是
扩展CollectionBase并使用常用方法:此[int index],
添加,插入,删除和包含。这个类与我使用的其他生成类之间没有区别,而不是包含对象的
类型。其他课程可以非常愉快地使用。

我敲了一个快速的控制台应用程序来测试这个系统的一个方面。我可以使用整数索引迭代列表成员,但不能使用foreach迭代。在循环内部调用GetName的方法不会改变底层对象,它只返回一个格式化的字符串。任何人都可以对这个特殊的怪癖有所了解吗?

//这个有用吗

UserList l = dataprovider.GetLocalUsers();
int c = l.Count;

while(c--> 0){
用户u = l [c];
Console.WriteLine(u.GetName( User.NameFormat.Initia lSurname));
}

//这会导致编译错误
// foreach(用户u in l)
// {
// Console.WriteLine(u.GetName(u.NameFormat.InitialSu rname));
//}



< blockquote> Flinchvoid写道:

我有一个类UserList,它是用python脚本自动生成的,它扩展了CollectionBase并且有通常的方法:this [int index],
添加,插入,删除和包含。这个类与我使用的其他生成类之间没有区别,而不是包含对象的
类型。其他类可以非常愉快地使用。



你必须实现IEnumerable接口以启用foreach to

枚举集合。


Anders Nor's
http ://dotnetjunkies.com/weblog/anoras/


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.Initia lSurname));
}
// this causes a compiler error

// foreach(User u in l)
// {
// Console.WriteLine(u.GetName(u.NameFormat.InitialSu rname));
// }

解决方案

Flinchvoid wrote:

Interesting little problem; go easy on me because I''m just a humble
scripter turned c# developer.


<snip>

Does your user list collection class implement IEnumerable ?

--
Lasse V?gs?ther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x0270466B


extending CollectionBase isn''t enough - you need to implement
GetEnumerator - see MSDN on foreach

Flinchvoid wrote:

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.Initia lSurname));
}
// this causes a compiler error

// foreach(User u in l)
// {
// Console.WriteLine(u.GetName(u.NameFormat.InitialSu rname));
// }



Flinchvoid wrote:

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.


You must implement the IEnumerable interface to enable foreach to
enumerate the collection.

Anders Nor?s
http://dotnetjunkies.com/weblog/anoras/


这篇关于编译错误与foreach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆