如何使通过引用传递方法工作

我在一个类中编写了一个引用方法,并在public partial class DYBook : Form中调用它,但它不起作用,值也没有改变。如果我输入postCode.Text而不是temp,将会返回一个错误。我不知道这里出了什么问题。

public static bool DYPostalCodeValidation(ref string inputField)
{
    Regex pattern = new Regex(@"^([A-Za-z]\d[A-Za-z][\s]?\d[A-Za-z]\d)$");
    if (inputField == "" || inputField == null)
    {
        return false;
    }
    else if (pattern.IsMatch(inputField))
    {
        inputField = inputField.ToUpper();
        if (inputField.Length == 6)
        {
            inputField = inputField.Insert(3, " ");
        }
        return true;
    }
    else
    {
        return false;
    }
}

在这里调用它

Private string temp;
temp = postalCode.Text;

if (DYValidation.DYPostalCodeValidation(ref temp) == true)
{
    postalCode.Text = temp;
}
else
{
    postalCode.Focus();
    errorMessage.Text += "Postal Code is invalid" + "\n";
}

转载请注明出处:http://www.gztaineng.com/article/20230526/1404559.html