MikeMCSD
asked on
convert to bool in datalist
How can I convert this string to a bool? thanks
bool active = ((HiddenField)e.Item.FindC ontrol("hi dActive")) .Value;
this didn't work:
bool active = (bool)((HiddenField)e.Item .FindContr ol("hidAct ive")).Val ue;
bool active = ((HiddenField)e.Item.FindC
this didn't work:
bool active = (bool)((HiddenField)e.Item
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?
if string is null then false else true
sort of thing?
ASKER
string active = ((HiddenField)e.Item.FindC ontrol("hi dActive")) .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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
... ")).Value == "True";
Isn't that setting the value to True everytime?
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-
== is to compare. = would assign-
ASKER
it give the error: Cannot implicitly convert type 'bool' to 'string'
ASKER
foget that, . . I had string active . . instead of bool active . .
it works, . . but don't really understand the logic
it works, . . but don't really understand the logic
Are you SURE it is a string?
That error suggests it is already a bool.
That error suggests it is already a bool.
>it works, . . but don't really understand the logic
bool active = ((HiddenField)e.Item.FindC ontrol("hi dActive")) .Value == "True";
it will evaluate like this:
Q: ((HiddenField)e.Item.FindC ontrol("hi dActive")) .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"
bool active = ((HiddenField)e.Item.FindC
it will evaluate like this:
Q: ((HiddenField)e.Item.FindC
-> 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"
ASKER
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
>>Are you SURE it is a string?
>>That error suggests it is already a bool.
you're right, . . this is really confusing now, lol
what is the string value actually?