protected void calculateRateButton_Click(object sender, EventArgs e)
{
//declare variables and retrieve user inputs
int nbPeopleInt;
string roomTypeString;
int nbNightsInt;
nbNightsInt = int.Parse(nbNightsTextBox.Text);
nbPeopleInt = int.Parse(nbPeopleDropDownList.Text);
roomTypeString = roomTypeDropDownList.Text;
int totalFareInt = calculateRoomBooking(roomTypeString, nbPeopleInt, nbNightsInt);
}
private int calculateRoomBooking(string RoomType, int nbNights, int nbPeople)
{
int totalFareInt = 0;
int dailyRateInt = 0;
// set daily rate based on selected room type
switch (RoomType)
{
case "Standard":
dailyRateInt = 100;
break;
case "Superior":
dailyRateInt = 150;
break;
case "Luxury":
dailyRateInt = 175;
break;
}
// calculation of booking rate
totalFareInt = dailyRateInt * nbNightsInt + 10 * (nbPeopleInt - 1) * nbNightsInt;
// display booking rate
totalRateTextBox.Text = totalFareInt.ToString();
}
protected void roomDetailsButton_Click(object sender, ImageClickEventArgs e)
{
// go to room details page
Response.Redirect("RoomDetails.aspx");
}
然而,它总是说dailyRateInt,nbNightsInt,nbPeopleInt没有用这一行定义:
totalFareInt = dailyRateInt * nbNightsInt + 10 * (nbPeopleInt - 1) * nbNightsInt;
但是我确实在上面定义了它们,你们可以看到
请帮忙
答案 0 :(得分:2)
变量nbPeopleInt
是calculateRateButton_Click
的局部变量,它在方法范围之外使用。因此,你得到了这个问题。
您可能在nbPeopleInt
方法之外定义roomTypeString
,nbNightsInt
和calculateRateButton_Click
您可能想要这样做: -
private int calculateRoomBooking(string RoomType, int nbNights, int nbPeople)
{
int totalFareInt = 0;
int dailyRateInt = 0;
// set daily rate based on selected room type
switch (RoomType)
{
case "Standard":
dailyRateInt = 100;
break;
case "Superior":
dailyRateInt = 150;
break;
case "Luxury":
dailyRateInt = 175;
break;
}
// calculation of booking rate
totalFareInt = dailyRateInt * nbNights + 10 * (nbPeople - 1) * nbNights;
// USING nbNights AND nbPeople instead of nbNightsInt and nbPeopleInt
// display booking rate
totalRateTextBox.Text = totalFareInt.ToString();
return 1; //Change 1 to the value which you want to return from the function.
}
答案 1 :(得分:1)
当函数参数为nbNightsInt
和nbPeopleInt
nbNights
和nbPeople
做的:
private int calculateRoomBooking(string RoomType, int nbNights, int nbPeople)
{
int totalFareInt = 0;
int dailyRateInt = 0;
// set daily rate based on selected room type
switch (RoomType)
{
case "Standard":
dailyRateInt = 100;
break;
case "Superior":
dailyRateInt = 150;
break;
case "Luxury":
dailyRateInt = 175;
break;
}
// calculation of booking rate
totalFareInt = dailyRateInt * nbNights + 10 * (nbPeople - 1) * nbNights;
// USING nbNights AND nbPeople instead of nbNightsInt and nbPeopleInt
// display booking rate
totalRateTextBox.Text = totalFareInt.ToString();
}
答案 2 :(得分:0)
您正在尝试使用变量'nbNightsInt'和'nbPeopleInt'而不是'nbNights'和'nbPeople',并且那些2在该范围内不存在。只需更改该行上的变量名称:
totalFareInt = dailyRateInt * nbNights + 10 * (nbPeople - 1) * nbNights;