1

私はボート係留予約プログラムを持っています。現在、その仕組みは、名前を入力してボタンをクリックすると、次の利用可能な係留が表示されます.6つの桟橋と桟橋ごとに5つの係留があります.

それで、すべての係留と桟橋が使い果たされた後、行き場がないためにプログラムがクラッシュします。利用可能なスポットがもうないことをユーザーに伝えるメッセージボックスを取得するにはどうすればよいですか?

これは私のコードです:

ボタンクリック:

private void button1_Click(object sender, EventArgs e)
{
    var req = new BoatRequest();
    req.Name = txtName.Text;
    var client = new BoatReserveSoapClient();
    BoatResponse response = client.ReserveMooring(req);

    if (req != null)
    {
        Pierlbl.Text = response.Pier.ToString();
        Mooringlbl.Text = response.Mooring.ToString();
        Pierlbl.Visible = true;
        Mooringlbl.Visible = true;
    }
    else
    {
        Pierlbl.Text = "Error Occured, please try again";
        Mooringlbl.Text = "Error Occured, please try again";
    }
}

Web メソッド:

//Setting the max value for Piers and Moorings
public const int maxPiers = 6;
public const int maxMoorings = 30;
private static bool[,] reserveMooring = new bool[maxPiers, maxMoorings];

[WebMethod]
public BoatResponse ReserveMooring(BoatRequest req)
{
    var res = new BoatResponse();

    //if pierCalc set to 0, if smaller than maxPiers increment
    for (int pierCalc = 0; pierCalc < maxPiers; pierCalc++)
    {
        //if mooringCalc set to 0, if smaller than maxMoorings increment
        for (int mooringCalc = 0; mooringCalc < maxMoorings / maxPiers; mooringCalc++)
        {
            if (!reserveMooring[pierCalc, mooringCalc])
            {
                reserveMooring[pierCalc, mooringCalc] = true;
                res.Pier = (Pier)pierCalc;
                res.Mooring = (Mooring)mooringCalc;
                return res;
            }
        }
    }
    return null;
}

これはクラッシュする場所です:

Pierlbl.Text = response.Pier.ToString();
4

3 に答える 3

2

response次のように、が null でないことを確認します。

if (response != null)
{
    Pierlbl.Text = response.Pier.ToString();
    Mooringlbl.Text = response.Mooring.ToString();
    Pierlbl.Visible = true;
    Mooringlbl.Visible = true;
}
else
{
    Pierlbl.Text = "Error Occured, please try again";
    Mooringlbl.Text = "Error Occured, please try again";
}
于 2013-10-28T14:11:45.480 に答える
0

この例では、reqがnullと等しくなることはありません。応答オブジェクトは Web サービスから戻り値を受け取っているため、if ステートメントで null 値をチェックする必要があります。

そのようです:

 if (response != null)
        {
            //Logic for success
        }
        else
        {
            //Logic for failure
        }
于 2013-10-28T14:19:14.550 に答える