473,322 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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").ToS tring());

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

May 30 '07 #1
3 3949
On May 30, 11:35 am, teddysn...@hotmail.com wrote:

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

May 30 '07 #2
On May 30, 11:49 am, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On May 30, 11:35 am, teddysn...@hotmail.com wrote:

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

May 30 '07 #3
On May 30, 12:02 pm, teddysn...@hotmail.com wrote:

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

May 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Yogi_Bear_79 | last post by:
I'm Creating new Keys in the registry, but when I do, they always have a Vaule called (Default) type RG_SZ. I need them to be DWord dword:00000005 Is there a way to automaticaly make the...
6
by: Bry | last post by:
I'm having problems writing (and reading) boolean data to the registry. // Write a boolean value to the registry // I've not included the obvious bits of code in these samples bool myBool =...
6
by: CMG | last post by:
I am writing a little code to associate an extention with my program. And as far as i can see, i need to do the following: Public Function associatefile(ByVal FILE_EXTENTION_TO_ASSOCIATE As...
9
by: Just Me | last post by:
Public Shared Function GetValue(ByVal keyName As String, ByVal valueName As String, Optional ByVal badKey As Object = "") As Object Dim UserKey As RegistryKey =...
3
by: Joe Delphi | last post by:
Hi, I want to store non-string values in the registry. I have a few Boolean flags and some integer numbers that I want to store. It appears that the SaveSettings command only handles the...
5
by: Parv | last post by:
I am working in a domain environment. I am on a client machine and wants to edit registery of domain server. I am currently in a Domain user account and member of Domain administrators on the...
9
by: Newbie Coder | last post by:
Hello Newsgroup Readers I would like to know how to go & do the following: I have a certain registry key that has sub values Example: Key1 http://www.microsoft.com Key2 ...
4
by: RhavoX | last post by:
Hi. This may be a very stupid question but I'll leave you to judge it ;) I know there were lots of questions about this but none of the answers suits me. I'm wondering how to get the BINARY type...
8
by: Kniffel | last post by:
hi everyone I programm an ISAPI extension with embedded Visual C++ for a Windows CE device. I try to write the network settings to the registry. I am able to write the IpAdress to the registry,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.