在断开循环时检测

时间:2013-04-23 13:21:36

标签: c# .net

任何人都可以提供帮助,因为我在这里碰到了一堵墙,如何在因无效值而突破循环时如何检测。

例如,如果有人为孩子输入年龄为3 5 6 7 9就可以了,如果输入o 3 5 6 7,这将在我的代码中返回-1并退出循环。

如何检测并返回消息

public static int IntIsValid(string p)
        {
            int pn;
            bool isNum = int.TryParse(p, out pn);
            int pnIsvalid = isNum ? pn : -1;
            return pnIsvalid;
        }

string addDash  = Regex.Replace(agesOfChildren, " ", "_");
            string[] splitNumberOfChildren  = addDash.Split('_');
            string splitChildrensAge        = string.Empty;
            int checkAgeOfChildren          = 0;
            string problem = string.Empty;
            foreach (var splitAgeOfChild in splitNumberOfChildren)
            {
                splitChildrensAge           = splitAgeOfChild;
                checkAgeOfChildren          = RemoveInvalidInput.IntIsValid(splitChildrensAge);
                if (checkAgeOfChildren == -1)
                {
                    problem = "problem with age, stop checking";
                    break;
                }
            }

所以我想做像

这样的事情
if(error with loop == true)
{
ViewBag.Message = problem;
}

希望有人可以提供帮助,因为我已经空白了

乔治

5 个答案:

答案 0 :(得分:6)

很简单,你已经自己给出了答案:

boolean error_with_loop = false;
foreach (var splitAgeOfChild in splitNumberOfChildren) {
      splitChildrensAge           = splitAgeOfChild;
      checkAgeOfChildren          = RemoveInvalidInput.IntIsValid(splitChildrensAge);
      if (checkAgeOfChildren == -1)
      {
           error_with_loop = true;
           problem = "problem with age, stop checking";
           break;
      }
}

if (error_with_loop) {
    ViewBag.Message = problem;
}

或者你抛出Exception

try {
    foreach (var splitAgeOfChild in splitNumberOfChildren) {
          splitChildrensAge           = splitAgeOfChild;
          checkAgeOfChildren          = RemoveInvalidInput.IntIsValid(splitChildrensAge);
          if (checkAgeOfChildren == -1)
          {
                // note: no need to break
                throw new ErrorWithLoopException();
          }
    }
} catch (ErrorWithLoopException e) {
    ViewBag.Message = problem;
}

实际上,您似乎使用某种整数验证。 Int32类包含例外:http://msdn.microsoft.com/en-us/library/system.int32.parse%28v=vs.71%29.aspx您的代码实际上会更短(不知道它是否适用,因为我不知道您的验证代码会做什么额外的):

try {
    foreach (var splitAgeOfChild in splitNumberOfChildren) {
          splitChildrensAge           = splitAgeOfChild;
          checkAgeOfChildren          = Int32.Parse(splitChildrensAge);
    }
} catch (FormatException e) {
    ViewBag.Message = problem;
}

答案 1 :(得分:2)

您可以抛出异常(取决于程序的设计),也可以设置标志。

bool success = true;

foreach (var splitAgeOfChild in splitNumberOfChildren)
{
      splitChildrensAge = splitAgeOfChild;
      checkAgeOfChildren = RemoveInvalidInput.IntIsValid(splitChildrensAge);
      if (checkAgeOfChildren == -1)
      {
           problem = "problem with age, stop checking";
           success = false; // change the flag
           break;
      }
}

或者使用例外(我在这里使用过ArgumentOutOfRangeException但您可以创建自己的例外):

foreach (var splitAgeOfChild in splitNumberOfChildren)
{
      splitChildrensAge = splitAgeOfChild;
      checkAgeOfChildren = RemoveInvalidInput.IntIsValid(splitChildrensAge);
      if (checkAgeOfChildren == -1)
      {
           problem = "problem with age, stop checking";
           throw new ArgumentOutOfRangeException("Age", problem);
      }
}

答案 2 :(得分:1)

为什么不在最后检查string.Empty?只有在出现错误时才会设置:

if (!string.IsNullOrEmpty(problem))
{
     ViewBag.Message = problem;
}

答案 3 :(得分:1)

简单地说:

if (checkAgeOfChildren == -1)
{
  problem = "problem with age, stop checking";
  break;
}

if (!string.IsNullOrEmpty(problem)) {
   ViewBag.Message = problem;
}

你也可以抛出异常:

try { 
   if (checkAgeOfChildren == -1)
   {
      throw new ArgumentException("Invalid Argument");
   }  catch (ArgumentException e) 
   {
       ViewBag.Message = e.Message;
   }
}

ArgumentException可以替换为更合适或自定义的例外。

答案 4 :(得分:1)

为什么不在循环中喜欢这个:

 if (checkAgeOfChildren == -1)
                {
                    ViewBag.Message  = "problem with age, stop checking";
                    break;
                }