Having trouble with some bool/string issues

爷,独闯天下 提交于 2020-04-19 18:02:23

问题


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;

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;

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:

  • 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 = ?

回答1:


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"] );



来源:https://stackoverflow.com/questions/25666503/having-trouble-with-some-bool-string-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!