首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >只要If语句为真,循环就会停止,我不知道如何继续循环

只要If语句为真,循环就会停止,我不知道如何继续循环
EN

Stack Overflow用户
提问于 2019-11-19 18:23:39
回答 1查看 96关注 0票数 0

一旦程序代码检查了excel文件中第一个无效的列,它就不会继续检查其余的列。有没有办法继续循环并检查其余的列?

ValidationFile代码如下所示。它检查Excel的列名,并检查列的长度是否不超过128个字符并且不为空,或者作为一个整体不为空。

代码语言:javascript
运行
复制
 public Boolean InvalidColumnNames()
        {
            using (ReadAndStreamFile())
            {
                reader.Read();
                {
                    int counter = 0;
                    var ColumnsNames = Enumerable.Range(0, reader.FieldCount).Select(i => reader.GetValue(i)).ToList();
                    if (ColumnsNames.Count != 0 && reader.Read() == true)
                    {
                        for (int columnNumber = 0; columnNumber < ColumnsNames.Count; columnNumber++)
                        {
                            var excel = new ExcelDataReaderFile();
                            var cellAddress = excel.GetAddress(counter, 0);
                            counter += 1;
                            if (ColumnsNames[columnNumber] != null && ColumnsNames[columnNumber].ToString().Length > columnNameSizeLimit)
                            {
                               Console.WriteLine($"[{GetFileName(file)}]{reader.Name}!{cellAddress} is {columnNumber.ToString().Length} characters long and exceeds {columnNameSizeLimit} character column name limit. Supply a valid column name.");
                                return true;
                            }
                            else if (ColumnsNames[columnNumber] == null)
                            {
                                Conseol.WriteLine($"[{GetFileName(file)}]{reader.Name}!{cellAddress} is empty. Supply a valid column name.");
                                return true;
                            }
                            else
                            {
                               // return true;
                            }
                            continue;
                        }
                    }
                    else
                    {
                        Console.WriteLine($"[{GetFileName(file)}]{reader.Name} is empty and cannot be validated. Supply a non-empty file.");
                        return true;
                    };
                }
                reader.Dispose();
                reader.Close();
                return false;
            }
        }

下面是Program Main方法,它描述了如果无效的列名为true,则对这样的文件进行消息验证,否则就会通过验证。

代码语言:javascript
运行
复制
 class program
        {
            public static void Main(string[] args)
            {
                string path = ConfigurationManager.AppSettings["path"].ToString();
                string search = ConfigurationManager.AppSettings["search"].ToString();


                ExcelDataReaderFile ExcelDataReaderFile = new ExcelDataReaderFile();
                string[] fileEntries = Directory.GetFiles(path, "*" + search + "*", SearchOption.AllDirectories);

                dynamic counter = 0;
                BasicValidation BasicValidation = new BasicValidation();
                    foreach (string file in fileEntries)
                    {
                        BasicValidation basicValidation = new BasicValidation(file, 128, 32767, 128, 128);
                        counter += 1;

                        Logger.Info($"Validating {basicValidation.GetFileName(file)}");

                        //Console.ForegroundColor = ConsoleColor.Blue;
                        if (basicValidation.InvalidColumnNames())
                      {
                            //if the above statments are true then run those methods and log an error
                            //Console.WriteLine($"{counter}) {basicValidation.GetFileName(file)} in {Regex.Unescape(path)} has failed validation. Check the error log for details.");
                            Console.WriteLine("Press any key to continue...");
                            Console.ReadKey();
                        }
                        else
                        {
                            Console.WriteLine($"{counter}) {basicValidation.GetFileName(file)} in {file} has passed validation checks.");
                            Console.WriteLine("Press any key to continue...");
                            Console.ReadKey();
                        }
                }
                else
                {
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadKey();
                }
     }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-19 18:40:27

如果在任何for/foreach循环中编写return语句,循环将终止执行,并从那里返回。因此,使用变量并根据条件为其赋值,一旦循环完成,就会从方法中执行return

代码语言:javascript
运行
复制
        public Boolean InvalidColumnNames()
        {
            bool errorInFile = false;
            using (ReadAndStreamFile())
            {
                reader.Read();
                {
                    int counter = 0;
                    var ColumnsNames = Enumerable.Range(0, reader.FieldCount).Select(i => reader.GetValue(i)).ToList();
                    if (ColumnsNames.Count != 0 && reader.Read() == true)
                    {
                        for (int columnNumber = 0; columnNumber < ColumnsNames.Count; columnNumber++)
                        {
                            var excel = new ExcelDataReaderFile();
                            var cellAddress = excel.GetAddress(counter, 0);
                            counter += 1;
                            if (ColumnsNames[columnNumber] != null && ColumnsNames[columnNumber].ToString().Length > columnNameSizeLimit)
                            {
                                Console.WriteLine($"[{GetFileName(file)}]{reader.Name}!{cellAddress} is {columnNumber.ToString().Length} characters long and exceeds {columnNameSizeLimit} character column name limit. Supply a valid column name.");
                                errorInFile =  true;
                            }
                            else if (ColumnsNames[columnNumber] == null)
                            {
                                Conseol.WriteLine($"[{GetFileName(file)}]{reader.Name}!{cellAddress} is empty. Supply a valid column name.");
                                errorInFile =  true;
                            }
                            else
                            {
                                // return true;
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine($"[{GetFileName(file)}]{reader.Name} is empty and cannot be validated. Supply a non-empty file.");
                        errorInFile =  true;
                    };
                }
                reader.Dispose();
                reader.Close();

                return errorInFile;
            }
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58931735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档