问题
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