Going through hoops to get a registry DWORD to populate a Boolean

T

teddysnips

I have a notional Boolean value stored in the Registry. Actually it's
a REG_DWORD capable of taking values of either 0 or 1.

So, I need to populate a checkbox on a form dependent on this value.

1. I tried this:

chkLogToFile.Checked =
Boolean.Parse(LogFileKey.GetValue("LogToFile"));

No luck - "The best overloaded method match for 'bool.Parse(string)'
has some invalid arguments"

2. I tried this:

chkLogToFile.Checked =
Boolean.Parse(LogFileKey.GetValue("LogToFile").ToString());

No luck - "String was not recognized as a valid Boolean"

3. I tried this:

chkLogToFile.Checked = (bool)LogFileKey.GetValue("LogToFile");

No luck - "Specified cast is not valid"
NOTE: If I run this in the Debug ("Immediate") pane I get "Cannot
unbox 'LogFileKey.GetValue("LogToFile")' as a 'bool'"

4. I tried this:

chkLogToFile.Checked =
(bool)LogFileKey.GetValue("LogToFile").ToString();

No luck - "Cannot convert type 'string' to 'bool'"

5. I tried this:

chkLogToFile.Checked =
Convert.ToBoolean(LogFileKey.GetValue("LogToFile"));

Hooray!

I realise that this is likely my woeful ignorance, but this is the
first time that I've come across a language that wilfully refuses to
equate numeric 0 with boolean false.

Edward
 
J

Jon Skeet [C# MVP]

On May 30, 11:35 am, (e-mail address removed) wrote:

I realise that this is likely my woeful ignorance, but this is the
first time that I've come across a language that wilfully refuses to
equate numeric 0 with boolean false.

I guess you haven't used Java either then.

It's a *good thing* that it refuses to equate 0 with false, IMO. It
means that:

int i=GetSomeValue();

if (i=0)
{
}

will fail to compile (with a hard error, not just a warning) because
the type of the expression is boolean, not integer.

However, that's not what your previous attempts showed at all - the
return value of RegistryKey.GetValue isn't int, it's object. What
exactly would you want - an implicit conversion from object to bool?
Ick.

A better solution to your problem might be:

chkLogToFile.Checked = (int)LogFileKey.GetValue("LogToFile") != 0;

Nice and simple, and it makes it perfectly clear:
a) That you expect to get back an integer
b) You expect to convert integers to booleans by comparing with 0.

Jon
 
T

teddysnips

On May 30, 11:35 am, (e-mail address removed) wrote:



I guess you haven't used Java either then.

It's a *good thing* that it refuses to equate 0 with false, IMO. It
means that:

int i=GetSomeValue();

if (i=0)
{

}

will fail to compile (with a hard error, not just a warning) because
the type of the expression is boolean, not integer.

However, that's not what your previous attempts showed at all - the
return value of RegistryKey.GetValue isn't int, it's object. What
exactly would you want - an implicit conversion from object to bool?
Ick.

A better solution to your problem might be:

chkLogToFile.Checked = (int)LogFileKey.GetValue("LogToFile") != 0;

Nice and simple, and it makes it perfectly clear:
a) That you expect to get back an integer
b) You expect to convert integers to booleans by comparing with 0.

Jon

You're right - I haven't used Java.

I would expect (silly me!) that GetValue would return whatever the
type of the key was - in this case DWORD. So in answer, no, I
wouldn't want an implicit conversion from object to bool, but why not
from int to bool?

Anyway, your solution appears to be much more stylish than mine, so
I'll shamelessly use it without attribution!

Edward
 
J

Jon Skeet [C# MVP]

On May 30, 12:02 pm, (e-mail address removed) wrote:

You're right - I haven't used Java.

I would expect (silly me!) that GetValue would return whatever the
type of the key was - in this case DWORD.

And it does, as a value (a boxed int). However, the method is
*declared* to return object, so that it can return strings, ints,
longs etc.

Now, I would *thoroughly* support RegistryKey having extra methods
GetInt32Value, GetInt64Value etc - but that's a different matter.
So in answer, no, I wouldn't want an implicit conversion from object to
bool, but why not from int to bool?

For the reason I gave before - implicit conversions to bool leave
conditional expressions open to typos.

I never need to write the unintuitive
if (0==i)
in C# just to avoid creating a typo bug, *because* this implicit
conversion doesn't exist. The only reason to do it in C/C++ is to
avoid accidentally using assignment.
Anyway, your solution appears to be much more stylish than mine, so
I'll shamelessly use it without attribution!

Feel free :)

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top