How can I check whether or not a registry key object is = Nothing?

  • Thread starter Christian Blackburn
  • Start date
C

Christian Blackburn

Hi Gang,
Sometimes when I try to open a registry it doesn't exist and therefore the
object reference is equal to nothing. However VB.NET 2003 doesn't allow the
syntax if
If objReg <> Nothing then
'do this
End if

Can somebody tell me another way to check if the key failed to open? I
realize I can just use error handling, but I prefer to make that the last
resort :).
Cheers,
Christian
 
D

David Browne

Christian Blackburn said:
Hi Gang,
Sometimes when I try to open a registry it doesn't exist and therefore the
object reference is equal to nothing. However VB.NET 2003 doesn't allow the
syntax if
If objReg <> Nothing then
'do this
End if

The operators = and <> are operators that work on objects. But Nothing
isn't an object. It's just a reference. Nothing is a reference that fails
to refer to an object. It is the "null pointer" of VB.

You need the operator for object reference identity: 'is'.

If not objReg is Nothing then
'do this
End if

'is' is an operator that operates on references, not objects. It tells you
whether two references are equal. Two references are equal just in case
they refer to the same object, or both are Nothing.

David
 
H

Herfried K. Wagner [MVP]

Hello,

Christian Blackburn said:
Sometimes when I try to open a registry it doesn't exist
and therefore the object reference is equal to nothing. However
VB.NET 2003 doesn't allow the syntax if
If objReg <> Nothing then
'do this
End if

\\\
If Not objReg Is Nothing Then
...
End If
///
 
C

Christian Blackburn

Thank you very much David and Herfried,
I'm sure I'll use what I just learned trillion times over :).
Cheers,
Christian
 
H

Herfried K. Wagner [MVP]

Hello,

Christian Blackburn said:
Thank you very much David and Herfried,
I'm sure I'll use what I just learned trillion times over :).

In addition to my previous post:

MSDN says that 'OpenSubKey' returns 'Nothing' if the key doesn't exist.
 
C

Christian Blackburn

Hi Herfried,
That is exactly what I said :).
In addition to my previous post:

MSDN says that 'OpenSubKey' returns 'Nothing' if the key doesn't exist.
Sometimes when I try to open a registry [key and] it doesn't exist and therefore the
object reference is equal to nothing.

Thanks so much for all the help Herfried you rate, I even added you to spell
check :).
Cheers,
Christian Blackburn
 
F

Fergus Cooney

Hi Herfried

|| Herfried, I even added you to spell check :).

What better compliment?! ;-)

Regards,
Fergus
MVP [Windows Keyboard, PC Power Switch]
 
C

Christian Blackburn

Hi Herfried,
It's just that Outlook Express doesn't recognize your name as a word. It's
no big deal it's just, because it's a foreign name. What I'm trying to say
is that I've had to write you thank you so many times that it was worth my
time to add you to spell check. What I'm trying to say with a little bit of
comedy is that you help me a lot and I appreciate it :).
Cheers,
Christian Blackburn
 
F

Fergus Cooney

Hi Herfried, Christian,

Lol, and I was just giving it a bit of emphasis. ;-)

Regards,
Fergus
 

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