Link to home
Start Free TrialLog in
Avatar of MikeMCSD
MikeMCSDFlag for United States of America

asked on

convert to bool in datalist

How can I convert this string to a bool? thanks

bool active = ((HiddenField)e.Item.FindControl("hidActive")).Value;

this didn't work:

bool active = (bool)((HiddenField)e.Item.FindControl("hidActive")).Value;
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

>How can I convert this string to a bool? thanks
what is the string value actually?
what is the definition of HiddenField ?
If it is a string, could you compare it against Null or an empty string?

if string is null then false else true

sort of thing?
Avatar of MikeMCSD

ASKER

     string active = ((HiddenField)e.Item.FindControl("hidActive")).Value;
      if (active == "True")  << this works, but I would rather use:
      if (active)  
      ...

<asp:HiddenField ID="hidActive" Value='<%# Eval("Active") %>' Visible="false" runat="server" />

Active is a bit field.
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
... ")).Value == "True";
Isn't that setting the value to True everytime?
no. only when  that hidden field string value equals to "True".
== is to compare. = would assign-
it give the error:  Cannot implicitly convert type 'bool' to 'string'
foget that, . . I had   string active . . instead of  bool active . .  
it works,  . . but don't really understand the logic
Are you SURE it is a string?

That error suggests it is already a bool.
>it works,  . . but don't really understand the logic

bool active = ((HiddenField)e.Item.FindControl("hidActive")).Value == "True";

it will evaluate like this:

Q: ((HiddenField)e.Item.FindControl("hidActive")).Value
 -> is that string value equal to the string "True"  (note: the STRING value "true", not the boolean value of True)

A: yes -> return true (boolean this time)
A: no  -> return false (boolean this time)

and assigns that return value into "bool active"

thanks angel for the explanation

>>Are you SURE it is a string?
>>That error suggests it is already a bool.

you're right, . . this is really confusing now, lol