遇到一些bool/string问题 [英] Having trouble with some bool/string issues
问题描述
我有一些像这样打开的代码:
I've got some code-behind that opens like this:
namespace MyNamespace
{
public partial class _Default : Page
{
public DropDownList DDL_Reporting_RunForDaily;
public bool _retrievedData = false;
我想在会话变量中使用_retrieveData,因此我从一开始就将其设置为false.现在,我有一个"protected void",我想在其中更改此变量的值,因此我在使用以下行:
I want to use _retrieveData in a session variable, so I'm setting it to false from the start. Now, I have a "protected void" where I want to change the value of this variable, so I'm using the line:
Session["_retrievedData"] = true;
最后,在另一个公共无效空间"中,我要检查会话变量的值,并且仅在该值设置为false时才运行它.所以,我有了代码:
Lastly, in another "public void" I want to check the value of the session variable and only run it if the value is set to false. So, I've got the code:
bool CanRun = (bool)Session["_retrievedData"];
if (CanRun == true)
{
CanRun = false;
return;
}
我的问题是,最后一段代码出现以下错误:
My problem is, I'm getting the following errors with that last piece of code:
- 无法将类型'bool'隐式转换为'string'
- 运算符'=='不能应用于类型为'string'的操作数,并且 '布尔'
- Cannot implicitly convert type 'bool' to 'string'
- Operator '==' cannot be applied to operands of type 'string' and 'bool'
我为什么会遇到这些错误的任何想法?当我尝试将"=="更改为"="时,出现错误消息:
Any ideas why I'm getting those errors? When I tried changing the "==" to "=", I got an error that said:
- 条件表达式中的赋值始终是常量;你的意思 使用==代替=?
- Assignment in conditional expression is always constant; did you mean to use == instead of = ?
推荐答案
我对Session对象并不十分熟悉.
但是对我来说,索引器似乎返回一个字符串.
试试这个:
bool CanRun = Boolean.Parse( Session["_retrievedData"] );
I'm not terribly familiar with the Session object.
But to me it seems that the indexer returns a string.
Try this:
bool CanRun = Boolean.Parse( Session["_retrievedData"] );
这篇关于遇到一些bool/string问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!