一个方法可以返回2个布尔值吗?

时间:2017-11-02 10:46:29

标签: c# asp.net

我有两个布尔值,我想从同一个方法返回。 这似乎不起作用。无论如何,它们总是被设置为假。

我的方法是否犯了错误,还是有其他方法可以做到这一点?

    bool isRetry = false;
    bool isContinue = false;

    BeginRetrieveVehicleInfo(isRetry, isContinue);

    if (!isContinue)
    {
        _beginInspectionButton.Enabled = true;
        return;
    }

    if (!isRetry)
    {
        BeginInspection();
    }

BeginRetrieveVehicleInfo 方法代码:

    PleaseWaitForm pleaseWaitDialog = new PleaseWaitForm("Registration plate read in progress...", "Please Wait");

    pleaseWaitDialog.Show();
    AlprListener AlprListener = new AlprListener();
    string plateString = AlprListener.RunServer();
    pleaseWaitDialog.Close();

    if (plateString == "No response received!" || plateString == "")
    {
        ApplusMessageBox.Show("No response received from Camera!", "Warning");
        return isRetry = false;
    }
    else if (plateString != _vehicle.RegistrationID)
    {
        DialogResult dialogResult = ApplusMessageBox.Show("Camera detected an invalid Registration Plate. Do you want to continue?", "Warning: Camera detected an invalid Registration Plate!", MessageBoxButtons.YesNo);
        if (dialogResult == DialogResult.Yes)
        {
            return isContinue = true;
        }
        else
        {
            return isContinue = false;
        }
    }
    else
    {
        return isRetry = false;
    }

1 个答案:

答案 0 :(得分:6)

默认情况下,参数通过值传递(甚至引用也是按值传递的,这会让你感到困惑)。

如果您希望更改对调用者可见,则必须通过引用传递 - 因此在两个参数名称之前添加ref,并将ref添加到呼叫站点:

BeginRetrieveVehicleInfo(ref isRetry, ref isContinue);

void BeginRetrieveVehicleInfo(ref bool x, ref bool y) {...}

如果它不需要使用输入值,它也可以是out

或者,考虑返回一个元组:

(bool X, bool Y) BeginRetrieveVehicleInfo() {
    ...
    return (oneBool, anotherBool);
}

var result = BeginRetrieveVehicleInfo();
if(result.X) {...}