我正在做这个项目,我被卡住了。我有一个表单,在表单上,我有一个下一步按钮和上一步按钮:
在我的HouseList
类中,我有这段代码,我想要做的是创建一个方法来获得下一个房子,另一个方法来获得前一个房子。我已经试了很久了。
public HouseList()
{
housesList = new List<House>();
housesList.Add(new House()
{
Address = "35 Twin Oaks",
City = "Glendale",
Zip = "MDN6L3",
AskingPrice = "328,743.00",
PictureFile = "",
AveragePrice = "490,747.80",
Total = "2,453,739.00"
});
housesList.Add(new House()
{
Address = "35 Helen Drive",
City = "OakDale",
Zip = "G6L5M4",
AskingPrice = "455,899.00",
PictureFile = "",
AveragePrice = "490,747.80",
Total = "2,453,739,00"
});
housesList.Add(new House()
{
Address = "4 RiverBank Rd",
City = "Brampton",
Zip = "L9H4L2",
AskingPrice = "699,999.00",
PictureFile = "",
AveragePrice = "490,747.80",
Total = "2,453,739,00"
});
}
public List<string> NextHouse()
{
List<string> house = new List<string>();
foreach (House h in housesList)
{
int index = 0;
string conv = Convert.ToString(index);
if (conv == house[1])
{
conv = house[1];
}
}
return house;
}
发布于 2019-04-12 03:51:05
您需要有一个索引来告诉您哪个索引是当前索引。最小索引的索引将被比较为0,而最大索引的索引将被比较为would List.Count-1。考虑到这一点,NextHouse
和PreviousHouse
应该返回House
而不是List<House>
。
public House NextHouse(){
if(currentIndex + 1 != houseList.Count)
currentIndex++;
return houseList[currentIndex]
}
public House PreviousHouse(){
if(currentIndex -1 >= 0)
currentIndex--;
return houseList[currentIndex];
}
因此,如果你在列表中已经是最后一所房子的时候请求下一所房子,它只会返回最后一所房子。当你在列表的第一个位置时,如果你请求上一个房子,它将返回第一个房子。
为此,您必须将currentIndex
初始化为类成员。
发布于 2019-04-12 03:50:07
这是下一项:
public List<House> housesList;
int currentIndex = 0;
public HouseList()
{
housesList = new List<House>();
housesList.Add(new House()
{
Address = "35 Twin Oaks",
City = "Glendale",
Zip = "MDN6L3",
AskingPrice = "328,743.00",
PictureFile = "",
AveragePrice = "490,747.80",
Total = "2,453,739.00"
});
housesList.Add(new House()
{
Address = "35 Helen Drive",
City = "OakDale",
Zip = "G6L5M4",
AskingPrice = "455,899.00",
PictureFile = "",
AveragePrice = "490,747.80",
Total = "2,453,739,00"
});
housesList.Add(new House()
{
Address = "4 RiverBank Rd",
City = "Brampton",
Zip = "L9H4L2",
AskingPrice = "699,999.00",
PictureFile = "",
AveragePrice = "490,747.80",
Total = "2,453,739,00"
});
}
public House NextHouse()
{
return housesList[(++currentIndex)%housesList.Count()];
}
这将在到达最后一项时循环。如果你想让它停止,改变逻辑。
https://stackoverflow.com/questions/55640003
复制相似问题