在c#中循环 - (对于每个) [英] loop through in c# - (for each)

查看:63
本文介绍了在c#中循环 - (对于每个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public delegate void FileDelegate(List<string>str);

public event FileDelegate AllFilesCompleted;

public ObservableCollection<FileUpload> files;

List<string> str = new List<string>();


void upload(object sender, EventArgs e)
{
FileUpload fu = sender as FileUpload;
if (fu.Status == FileUploadStatus.Complete)
{
if (uploading)
UploadFiles();
if (AllFilesCompleted != null)
{
if (files.Count == files.Count(q => q.Status == FileUploadStatus.Complete))
foreach (string data in files) //Error line
{
files.ElementAt(i).Name; //Error line
str.Add(data);
}
}
}
}




Hi,

When i build the program i got below errors. Please help me what i did mistake in code...





不能将类型'DC.FileUpload.FileUpload'转换为'string'



只有赋值,调用,递增,递减和新对象表达式才能用作语句



当前上下文中不存在名称i



Cannot convert type 'DC.FileUpload.FileUpload' to 'string'

Only assignment, call, increment, decrement, and new object expressions can be used as a statement

The name 'i' does not exist in the current context

推荐答案

您好,



首先:

Hello,

First:
public ObservableCollection<FileUpload> files;



您已将其声明为FileUploads的集合,并且在foreach中您尝试迭代字符串。



第二名:


You've declared it as collection of FileUploads and in foreach you are trying to iterate strings.

Second:

files.ElementAt(i).Name;



你没有宣布我。既然你正在使用foreach,我想你想在那里获取数据变量的值,因为它是该位置的元素。



希望它有所帮助。



问候,


You didn't declare i. Since you are using foreach, I guess that you want to take value of data variable there, since it's the element on that position.

Hope it helps.

Regards,


摆脱你的第一个错误...



foreach(文件中的字符串数据)更改为
To get rid of your first error...

Change foreach (string data in files) to
foreach (var data in files)

然后将鼠标悬停在单词 var 上 - 您将看到它的类型为 FileUpload - 正如Caldazar87指出的那样,该集合不是字符串的集合,而是FileUpload的集合。



第二个错误可能是因为你曾经有一个 循环使用迭代器 i 。您现在已经用 foreach 替换了它,并且 i 不再存在。您不需要使用 files.ElementAt ,因为 data 变量已经为您提供了该元素。



这引出了我的下一个问题 - 你没有用这个名字做任何事情 - 至少你似乎想把它分配给一个字符串变量,例如

Then hover your mouse over the word var - you will see that it is of type FileUpload - as Caldazar87 pointed out, the collection is not a collection of string, but a collection of FileUpload.

The second error is probably because you used to have a for loop here with an iterator of i. You have now replaced that with a foreach and i no longer exists. You don't need to use files.ElementAt as the data variable is already giving you that element.

Which leads me to the next issue you have - you are not doing anything with that name - at the very least you appear to want to assign it to a string variable e.g.

var theName = data.Name;





然后会出现另一个问题



Which will then present you with another problem in the line

str.Add(data);

- 您将 str 声明为列表< string>< / string> ; data 是一个 FileUpload 对象。我猜你只想要文件的名称,所以从

- you declared str as a List<string></string> but data is a FileUpload object. I'm guessing you just want the name of the file so change that part from

files.ElementAt(i).Name; //Error line
str.Add(data);

to

str.Add(data.Name);





最后,请确保您已阅读错误在尝试编译程序时完全列出 - 将列出所有这些错误,并且通常有足够的线索指出您的修复方向。如果您使用的是Visual Studio,则可以双击错误以转到出现故障的代码行。始终开始查看列表中的第一个错误,因为许多其他错误可能会在修复后消失。



Finally, make sure that you read the Error List fully when trying to compile your program - all of these errors would be listed and usually there is enough of a clue to point you in the direction of the fix. If you are using Visual Studio, you can double click on a error to take you to the line of code that is at fault. Always start looking at the first error on the list as many of the others may disappear once that is fixed.


这篇关于在c#中循环 - (对于每个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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