Any ideas on how to turn "edible" in the code into a session to display as a label on a different page. Help will be much appreciated.
关于如何将代码中的“可食用”转换为会话以在不同页面上显示为标签的任何想法。将非常感谢帮助。
The label will display message like yes can eat
标签将显示消息,如是可以吃
code bellow
代码吼叫
public int totalCalories()
{
return grams * calsPerGram;
}
public string getFruitInfo()
{
string s;
if (edible == true)
{
s = fName + " is good and it has " + totalCalories() +
"calories";
}
else
{
s = "Hands off! Not edible";
//edible = Sesion ["ediblesesion"] as bool;
// Session ["ediblesession"] = edible;
}
return s;
}
}
3 个解决方案
#1
14
You already have the code for setting the session variable in the comment inside the if
statement:
您已经有了在if语句中的注释中设置会话变量的代码:
Session["ediblesession"] = edible;
However, you probably want to set the session variable outside the if
statement, so that it gets a value even if the boolean value is true
.
但是,您可能希望在if语句之外设置会话变量,以便即使布尔值为true也会获取值。
When you want to read the value back in the other page, you will get the boolean value boxed in an object, so you would need to cast it back to a boolean value:
当你想在另一个页面中读回值时,你将得到一个对象中的布尔值,所以你需要将它强制转换为一个布尔值:
edible = (bool)Session["ediblesession"];
Be careful with the spelling. If you try to read a session variable with the name "ediblesesion"
(as in the code in your comment), you won't get the variable that you stored as "ediblesession"
, and the compiler can't tell you that you made a typo as it's not an identifier.
小心拼写。如果您尝试读取名为“ediblesesion”的会话变量(如注释中的代码所示),您将无法获得存储为“ediblesession”的变量,并且编译器无法告诉您已制作一个拼写错误,因为它不是标识符。
If you want to read the value, but might come to the page without having set the value first, you would need to check if it exists:
如果要读取值,但可能在没有先设置值的情况下进入页面,则需要检查它是否存在:
object value = Session["ediblesession"];
if (value != null) {
edible = (bool)value;
} else {
edible = false; // or whatever you want to do if there is no value
}
#2
4
Setting:
设置:
this.Session["Edible"] = myBoolean;
Getting:
获得:
myBoolean = (bool) this.Session["Edible"];
#3
0
bool? edible;
object object_edible = Session["session_edible"];
if (object_edible != null)
edible = object_edible as bool?;
else
edible = false;
'bool' is a non-nullable value type in c#, bool? is a nullable type
'bool'是c#中不可为空的值类型,bool?是一种可以为空的类型
#1
14
You already have the code for setting the session variable in the comment inside the if
statement:
您已经有了在if语句中的注释中设置会话变量的代码:
Session["ediblesession"] = edible;
However, you probably want to set the session variable outside the if
statement, so that it gets a value even if the boolean value is true
.
但是,您可能希望在if语句之外设置会话变量,以便即使布尔值为true也会获取值。
When you want to read the value back in the other page, you will get the boolean value boxed in an object, so you would need to cast it back to a boolean value:
当你想在另一个页面中读回值时,你将得到一个对象中的布尔值,所以你需要将它强制转换为一个布尔值:
edible = (bool)Session["ediblesession"];
Be careful with the spelling. If you try to read a session variable with the name "ediblesesion"
(as in the code in your comment), you won't get the variable that you stored as "ediblesession"
, and the compiler can't tell you that you made a typo as it's not an identifier.
小心拼写。如果您尝试读取名为“ediblesesion”的会话变量(如注释中的代码所示),您将无法获得存储为“ediblesession”的变量,并且编译器无法告诉您已制作一个拼写错误,因为它不是标识符。
If you want to read the value, but might come to the page without having set the value first, you would need to check if it exists:
如果要读取值,但可能在没有先设置值的情况下进入页面,则需要检查它是否存在:
object value = Session["ediblesession"];
if (value != null) {
edible = (bool)value;
} else {
edible = false; // or whatever you want to do if there is no value
}
#2
4
Setting:
设置:
this.Session["Edible"] = myBoolean;
Getting:
获得:
myBoolean = (bool) this.Session["Edible"];
#3
0
bool? edible;
object object_edible = Session["session_edible"];
if (object_edible != null)
edible = object_edible as bool?;
else
edible = false;
'bool' is a non-nullable value type in c#, bool? is a nullable type
'bool'是c#中不可为空的值类型,bool?是一种可以为空的类型