Search code examples
c#comboboxwebmethod

Forms GUI with 2 comboboxes to check an bool array in a webmethod


hi im having trouble finding a way to have a webmethod check to see if the selected spot from the combo boxes if false. the combo boxes is to select a pier and a mooring. in the array tru is taken false is free.(well thats what i hope will happen). here some of the webmethod ive done.

edit i need to write a webmethed that calls 2 ints from 2 comboboxes and then check if that slot in the array is free. and if its not free select the next avilable slot. the array is for the pier and moorings.

[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;
    }

Solution

  • I hope I got the jist of what you are working on. Basically, you want a recursion that tests your array for pier and mooring. If true, then reserve the mooring, if false, then move on to find an open one.

    Some assumptions:

    • ReserveMooringResponse is a class with 2 properties, Pier and Mooring.
    • ReserveMooringRequest is a class with 2 properties, Pier and Mooring.
    • Note that I hardcoded the pier and the mooring. You would replace this with (int)combobox1.SelectedValue and (int)combobox2.SelectedValue
    • I moved the WebMethod attribute to the CheckReservation method.

    Here is the code:

    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);
        }