Avatar of MikeMCSD
MikeMCSD
Flag 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;
C#ASP.NET.NET Programming

Avatar of undefined
Last Comment
MikeMCSD

8/22/2022 - Mon
Guy Hengel [angelIII / a3]

>How can I convert this string to a bool? thanks
what is the string value actually?
Arragorn

what is the definition of HiddenField ?
Richard Quadling

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?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
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
Guy Hengel [angelIII / a3]

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
MikeMCSD

ASKER
... ")).Value == "True";
Isn't that setting the value to True everytime?
Guy Hengel [angelIII / a3]

no. only when  that hidden field string value equals to "True".
== is to compare. = would assign-
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
MikeMCSD

ASKER
it give the error:  Cannot implicitly convert type 'bool' to 'string'
MikeMCSD

ASKER
foget that, . . I had   string active . . instead of  bool active . .  
it works,  . . but don't really understand the logic
Richard Quadling

Are you SURE it is a string?

That error suggests it is already a bool.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Guy Hengel [angelIII / a3]

>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"

MikeMCSD

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