1

嗨,我很难找到一种方法来让 webmethod 检查组合框中的选定位置是否为假。组合框是选择一个码头和一个系泊。在数组 tru 中取 false 是免费的。(这就是我希望发生的事情)。这里我完成了一些 webmethod。

编辑我需要编写一个从 2 个组合框调用 2 个整数的 webmethed,然后检查数组中的该插槽是否空闲。如果它不是免费的,请选择下一个可用的插槽。该阵列用于码头和系泊处。

[WebMethod]
    public ReserveMooringResponse getMooring(ReserveMooringRequest req)
    {
        var resv = new ReserveMooringResponse();

        int pier = 0;
        int mooring = 0;
        bool checkSeat = false;
        string firstName;

        bool[,] mooringArray = new bool[,] {{ true, false, true, false, true}, //pier 1
                                        {true, false, false, true, true}, //pier 2
                                        {false, true, true, false, true}, //pier 3
                                        {false, false, true, false, true}, //pier 4
                                        {true, false, false, true, true},  //pier 5
                                        {true, true, false, false, false}}; //pier 6


        return resv;
    }
4

1 回答 1

0

我希望我能了解你正在做的事情。基本上,您需要一个递归来测试您的阵列的码头和系泊。如果为真,则保留系泊,如果为假,则继续寻找一个开放的系泊。

一些假设:

  • ReserveMooringResponse是一个具有 2 个属性的类,即 Pier 和 Mooring。
  • ReserveMooringRequest是一个具有 2 个属性的类,即 Pier 和 Mooring。
  • 请注意,我对码头和系泊进行了硬编码。您可以将其替换为(int)combobox1.SelectedValueand(int)combobox2.SelectedValue
  • 我将WebMethod属性移到CheckReservation方法中。

这是代码:

public partial class Form4 : Form
{
    public Form4()
    {
        InitializeComponent();

        var pier = 3;
        var mooring = 3;
        //set up client
        var service = new Service1();
        var resv = service.CheckReservation(pier, mooring);

        label1.Text = string.Format("Reservation for Pier {0} and Mooring {1} confirmed.", resv.Pier, resv.Mooring);
    }
}

public class Service1 : System.Web.Services.WebService
{
    bool[,] mooringArray = new bool[,] {{ true, false, true, false, true}, //pier 1
                                    {true, false, false, true, true}, //pier 2
                                    {false, true, true, false, true}, //pier 3
                                    {false, false, true, false, true}, //pier 4
                                    {true, false, false, true, true},  //pier 5
                                    {true, true, false, false, false}}; //pier 6

    public ReserveMooringResponse getMooring(ReserveMooringRequest req)
    {
        var resv = new ReserveMooringResponse();

        if (req.mooring > 4)
        {
            req.mooring--;
            req.pier++;
        }

        resv.Pier = req.pier;
        resv.Mooring = req.mooring;

        if (!mooringArray[req.pier, req.mooring])
        {
            var mooring = req.mooring;
            mooring++;
            resv = this.getMooring(new ReserveMooringRequest
            {
                mooring = mooring,
                pier = req.pier
            });
        }

        mooringArray[resv.Pier, resv.Mooring] = false;
        return resv;
    }

    [WebMethod]
    public ReserveMooringResponse CheckReservation(int pier, int mooring)
    {
        var req = new ReserveMooringRequest
        {
            pier = pier,
            mooring = mooring
        };
        return this.getMooring(req);
    }
于 2013-10-23T04:46:25.767 回答