尝试捕获后继续数据验证

时间:2016-04-13 21:32:23

标签: c# visual-studio-2012

好的,所以我在Visual Studio 2012中有一个表单。我想验证文本框中的用户输入以检查它们是否满足某些要求等。我有字符串,但最后一个要验证的是整数。 我想检查文本框中输入的数据是否为数字,如果不是,则向现有错误消息变量添加错误消息。如果是数字,我想检查它是否在某个范围内

当输入不是数字时,我的问题就出现了。例如,一个字符串。如果输入不是整数,我用来添加到错误消息的try catch工作,但在成功执行之后,代码继续并在尝试处理字符串时抛出异常。

这是所有代码:

    public string IDValid(string MotherBaseID,
                      string CodeName,
                      string AbilityRank,
                      string AssignedTo,
                      string Affiliation,
                      string HairColour,
                      string Ethnicity,
                      string DateJoined,
                      string AtMotherBase,
                      Int32 YearsAtBase)


///this function is used to validate the data in a new ID entry
///it accepts 9 parameters and returns a string containing the text of any errors (if any)
///if no errors, it returns a blank string
{
    string ErrorMsg; //var to store any error message
    ErrorMsg = ""; //initialise the var with a blank string

    //if the string is less than 1 char or more than 6
    if (MotherBaseID.Length < 1 | MotherBaseID.Length > 6)
    {
        //set the error message
        ErrorMsg = ErrorMsg + "The ID number must be between 1 and 6 characters, ";
    }

    if (CodeName.Length < 3 | CodeName.Length > 15)
    {
        //set the error message
        ErrorMsg = ErrorMsg + "The codename must be between 3 and 15 characters, ";
    }
    if (AbilityRank.Length < 2 | AbilityRank.Length > 3)
    {
        //set the error message
        ErrorMsg = ErrorMsg + "The ability rank must be between 2 and 3 characters, ";
    }
    if (AssignedTo.Length < 2 | AssignedTo.Length > 10)
    {
        //set the error message
        ErrorMsg = ErrorMsg + "The assigned to section must be between 2 and 10 characters, ";
    }
    if (Affiliation.Length < 7 | Affiliation.Length > 15)
    {
        //set the error message
        ErrorMsg = ErrorMsg + "The affiliation must be between 7 and 15 characters, ";
    }
    if (HairColour.Length < 3 | HairColour.Length > 10)
    {
        //set the error message
        ErrorMsg = ErrorMsg + "The hair colour must be between 3 and 10 characters, ";
    }
    if (Ethnicity.Length < 4 | Ethnicity.Length > 10)
    {
        //set the error message
        ErrorMsg = ErrorMsg + "The ethnicity must be between 4 and 10 characters, ";
    }

    if (AtMotherBase.Length < 3 | AtMotherBase.Length > 5)
    {
        //set the error message
        ErrorMsg = ErrorMsg + "The AtMotherbase field must be 'True' or 'False, ";
    }

    if (DateJoined.Length < 10 | DateJoined.Length > 11)
    {
        //set the error message
        ErrorMsg = ErrorMsg + "The date entered must be in the DD/MM/YYYY format, ";
    }

    try
    {
        //var to store the DateJoined 
        DateTime Temp;
        //assign the date to a temporary var
        Temp = Convert.ToDateTime(DateJoined);
    }
    catch
    {
        //set the error message
        ErrorMsg = ErrorMsg + "Date entered is not a valid date";
    }

    try
    {
        YearsAtBase = Convert.ToInt32(YearsAtBase);
    }
    catch
    {
        ErrorMsg = ErrorMsg + "Please enter a number into the YearsAtBase field ";
    }
    if (YearsAtBase < 1 | YearsAtBase > 15)
    {
        //set the error message
        ErrorMsg = ErrorMsg + "YearsAtBase must be a number between 1 and 15 ";
    }



        if (ErrorMsg == "")
        {
            //return a blank string
            return "";
        }
        else
        {
            //return the errors string value
            return "There were the following errors : " + ErrorMsg;
        }


    }

这是我遇到问题的代码:

try
    {
        YearsAtBase = Convert.ToInt32(YearsAtBase);
    }
    catch
    {
        ErrorMsg = ErrorMsg + "Please enter a number into the YearsAtBase field ";
    }
    if (YearsAtBase < 1 | YearsAtBase > 15)
    {
        //set the error message
        ErrorMsg = ErrorMsg + "YearsAtBase must be a number between 1 and 15 ";
    }

我希望代码检查YearsAtBase输入是否为整数,如果不是则告诉用户,但如果是,则正常执行代码,直到它返回结束验证的返回。我在这里傻了吗?

0 个答案:

没有答案