可以';无法解决预订服务中与枚举相关的显示选项
本文关键字:枚举 选项 显示 预订服务 解决 可以 | 更新日期: 2023-09-27 18:25:08
我有一个预订服务,如果您想显示所有座位、所有可用座位或所有预订座位,您可以通过枚举进行选择。
我的问题是,我不知道如何做到这一点,即只显示预订的座位,因为到目前为止,如果我选择"只显示预订座位",它会显示正确的预订座位数量,但它会从整个数组的开头迭代,而不是从我想要的开始迭代,所以如果有3个预订座位,它将显示座位0,1,2,而不是实际预订的座位。
我很确定,我需要将for (int i = 0; i < count; i++)
更改为for (int i = 0; i < totalNumberOfSeats; i++)
,以实际循环通过所有座位,而不是只显示我想要显示的座位数量,但之后我就脱离了绑定异常,我不知道如何继续。
public string[] GetSeatInfoStrings(DisplayOptions choice)
{
int count = GetNumOfSeats(choice);
if (count <= 0)
{
return null;
}
string[] strSeatInfoStrings = new string[count];
for (int i = 0; i < count; i++)
{
strSeatInfoStrings[i] = GetSeatInfoAt(i);
}
return strSeatInfoStrings;
}
public string GetSeatInfoAt(int index)
{
int row = GetRow(index);
int col = GetCol(index);
string seatInfo;
if (string.IsNullOrEmpty(GetName(m_nameMatrix[row, col])))
{
seatInfo = MarkAsVacant(row, col);
}
else
{
seatInfo = MarkAsReserved(row, col);
}
return seatInfo;
}
我有一个方法IsReserved(int index),并尝试了类似的方法
if (IsReserved(i))
{
// Want to return all seats that are reserved
}
else if (!IsReserved(i))
{
// Want to return all seats that are NOT reserved
}
就这种方法而言,它还可以,但问题是我不知道该在括号里放什么。
在不了解完整模型的情况下,我们无法解决这个问题。几乎没有什么细节。你可能想要这样的东西:
string[] strSeatInfoStrings = new string[count];
int counter = 0;
for (int i = 0; i < totalNumberOfSeats; i++)
{
var seatInfo = GetSeatInfoAt(i);
if (seatInfo == "reserved") //some kind of checking
{
strSeatInfoStrings[counter] = seatInfo;
counter++; //run another counter
}
}
return strSeatInfoStrings;
您可以避免阵列和计数器的所有麻烦,只需使用List<T>
。。
var strSeatInfoStrings = new List<string>();
for (int i = 0; i < totalNumberOfSeats; i++)
{
var seatInfo = GetSeatInfoAt(i);
if (seatInfo == "reserved") //some kind of checking
strSeatInfoStrings.Add(seatInfo);
}
return strSeatInfoStrings;
在这种情况下,使用List可能比使用数组更容易,因为在开始添加List之前,您不需要知道其大小。
public string[] GetSeatInfoStrings(DisplayOptions choice)
{
List<string> lstSeatInfoStrings = new List<string>();
for (int i = 0; i < totalNumberOfSeats; i++)
{
string seatInfo = GetSeatInfoAt(i);
if (seatInfo.Reserved)
{
lstSeatInfoStrings.Add(seatInfo);
}
}
if (lstSeatInfoStrings.Count == 0)
{
return null;
}
return lstSeatInfoStrings.ToArray();
}