【发布时间】:2018-06-26 08:13:06
【问题描述】:
我有一个文件夹系统,并且有一条规则不允许您将多个文件夹添加到同一个目录中。添加文件夹时我有以下逻辑。
// CHECK TO SEE IF FOLDER NAME ALREADY EXISTS
if (areaListViewIsVisible)
{
foreach (Folder folder in areaList)
{
if (pResult.Text == folder.Title)
{
await Application.Current.MainPage.DisplayAlert("Error", "You cannot add a folder with the same name.", "OK");
return;
}
}
}
else
{
if (productListViewVisible)
{
foreach (Folder item in productList)
{
if (pResult.Text == item.Title)
{
await Application.Current.MainPage.DisplayAlert("Error", "You cannot add a folder with the same name.", "OK");
return;
}
}
}
}
//
这似乎是一种不好的检查方式,您能推荐一种更好的方式吗?
谢谢!
【问题讨论】:
-
为新文件夹创建路径并检查该路径是否已存在。参考this
-
这个问题更像是一个代码审查请求,而不是一个中肯的问题描述。这不属于这里,也许它属于 Code Review。
-
只是将foreach循环重构为单独的检查函数?