-1

好的,按一下按钮,我的代码应该将系泊增加一,直到它达到五,在它达到五之后,它必须将码头增加一,然后系泊增加一直到它达到五和然后将码头增加一,依此类推。

但它目前所做的是,它将码头增加,然后系泊增加至 5,但在我单击按钮使其再次增加之后(它应该将码头增加至 2 并将系泊回增加至 1)它崩溃了。

这是我的代码

//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 mooringCalc set to 0, if smaller than maxMoorings increment
            for (int mooringCalc = 0; mooringCalc < maxMoorings; mooringCalc++)
            {
                //if pierCalc set to 0, if smaller than maxPiers increment
                for (int pierCalc = 0; pierCalc < maxMoorings / maxPiers; mooringCalc++)
                {

                    if (!reserveMooring[pierCalc, mooringCalc])
                    {
                        reserveMooring[pierCalc, mooringCalc] = true;
                        res.Pier = (Pier)pierCalc;
                        res.Mooring = (Mooring)mooringCalc;
                        return res;
                    }
                }
            }
            return null;
        }

崩溃发生在这里:

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);//error is on this line

这是BoatResponse

namespace NewBoatBookingApp
{

    public enum Pier
    {
        one,
        two,
        three,
        four,
        five,
        six
    }


    public enum Mooring
    { 
        one,
        two,
        three,
        four,
        five
    }

    public class BoatResponse
    {
        public Pier Pier;
        public Mooring Mooring;
    }
}

这是错误:

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Instance validation error: '6' is not a valid value for NewBoatBookingApp.Mooring.
4

3 回答 3

4

Mooring似乎很清楚 -当它仅从 1 变为 5 时,您尝试将值分配为 6。

看起来你的PiersMoorings循环是向后的?

//if mooringCalc set to 0, if smaller than maxMoorings increment
for (int pierCalc = 0; pierCalc < maxPiers; pierCalc++)
{
    //if pierCalc set to 0, if smaller than maxPiers increment
    for (int mooringCalc = 0; mooringCalc < maxMoorings / maxPiers; mooringCalc++)
于 2013-10-28T13:10:25.303 回答
1

你的方法中的外循环有一个条件

mooringCalc < maxMoorings

maxMoorings设置的30位置和您的枚举只有 6 个值。所以当你这样做时:

res.Mooring = (Mooring)mooringCalc; //here mooringCalc is 6

6由于枚举只有 5 个项目,因此您会得到一个例外值。因此错误

'6' 不是 NewBoatBookingApp.Mooring 的有效值。

于 2013-10-28T13:11:49.090 回答
0

看起来你正在以相当复杂的方式处理这个问题......

如果我理解正确,您有 30 个停泊处,它们位于 1 号至 6 号码头。

你为什么不把你的系泊当作一个连续的集合,找到一个空的,然后计算那个是哪个码头?

//Setting the max value for Piers and Moorings
    public const int mooringsPerPier = 5;
    public const int maxMoorings = 30;
    private staticbool[] reserveMooring = new bool[maxMoorings];

[WebMethod]
    public BoatResponse ReserveMooring(BoatRequest req)
    {
        int mooring = Array.IndexOf(reserveMooring, false);
        if (mooring < 0)
        {
          return null;
        }
        reserveMooring[mooring] = true;
        return new BoatResponse()
        {
           Pier = (Pier)(int(mooring / mooringsPerPier)),
           Mooring = (Mooring)(int(mooring % mooringsperPier)
        };
    }

同样,我会按照您在此处使用它们的方式丢失两个枚举,除非它们稍后会获得有用的名称:)

于 2013-10-28T13:56:40.027 回答