improving my writing - 2005

K

Kaypee

below is what I have as part of registry class


Public Function DoesKeyExist() As Boolean

Dim oReg As RegistryKey

Dim bExist As Boolean = False

Try

oReg = m_MSRegRoot.OpenSubKey(m_sKeyPath, False)

If Not oReg Is Nothing Then

bExist = True

End If

Catch ex As Exception

'

Finally

If Not oReg Is Nothing Then

oReg.Close()

oReg = Nothing

End If

End Try

Return bExist

End Function





2005 IDE warns 'Variable oReg is used before it has been assigned a value.
A null reference exception could result at runtime'



My question is - how am I supposed to do this?
 
T

Terry Olsen

When you declare the variable, assign it an initial value...such as:

dim oReg as RegistryKey = Nothing
 
K

Kaypee

:-0

OMG. I use that in most other places.

Why did my brain not associate that with the error message recieved?


Thanks Terry
 
C

Cor Ligthert [MVP]

"Kaypee"
Public Function DoesKeyExist() As Boolean
Dim oReg As RegistryKey
Dim bExist As Boolean = False
Try
oReg = m_MSRegRoot.OpenSubKey(m_sKeyPath, False)
If Not oReg Is Nothing Then
bExist = True
End If
Catch ex As Exception
'
Finally
If Not oReg Is Nothing Then
oReg.Close()
oReg = Nothing
End If
End Try
Return bExist
End Function
As the warning tells you have a look at the use of oReg, in most places you
have a lot of code around it, as far as I can see it now, does that code
nothing.

Cor
 
C

Cor Ligthert [MVP]

Terry,
dim oReg as RegistryKey = Nothing

I strongly disagree with you. The only thing what you do with this is
creating extra code and disable the checking on wrong used code.

Cor
 
G

Guest

Cor, I agree that there should be no need to assign a variable to nothing.
Is there a way to turn off these types of warning's in VB 2005? They would
be quite annoying. I haven't gotten VB2005 yet but do plan to do so when it
comes out and am trying to learn as much about it as I can from this group.
 
L

Larry Lard

I am confused...
below is what I have as part of registry class


Public Function DoesKeyExist() As Boolean
Dim oReg As RegistryKey
Dim bExist As Boolean = False
Try
oReg = m_MSRegRoot.OpenSubKey(m_sKeyPath, False)
If Not oReg Is Nothing Then
bExist = True
End If
Catch ex As Exception
'
Finally
If Not oReg Is Nothing Then
oReg.Close()
oReg = Nothing
End If
End Try
Return bExist
End Function

2005 IDE warns 'Variable oReg is used before it has been assigned a value.
A null reference exception could result at runtime'

Terry said:
When you declare the variable, assign it an initial value...such as:

dim oReg as RegistryKey = Nothing

Terry,

Perhaps you could explain to me why the IDE is complaining? As far as I
can see the variable is declared and then immediately assigned a value
before ever being accessed. So what's the problem?
 
C

Cor Ligthert [MVP]

Dennis,

Because I have placed something about this somewhere else, did I get a
message from Herfried. (We agree about this before you misunderstand it).

What you ask is in the properties from myproject in the solution explorer.

There you see when you open that a tabpage with tabs in front

There you can tell at 'compile' what is a 'warning' 'none' or an 'error'.

My opinion is that this is something that should be set at end of
programming to see if there are some things not completly done. And when the
warning is correct set of again to none, to prevent that more important
warnings are overseen.

I hope this helps,

Cor
 
C

Cor Ligthert [MVP]

Dennis

My message can be read wrong. The text is mine not from Herfried.

Cor
 
K

Kaypee

Larry

From what I can figure:

Larry Lard said:
I am confused...

it is possible that exception may occur in next line - thus nothing ever
assigned to oReg
execution moves onto Catch and then Finally

thus it may get here before oReg actually used?
 
C

Cor Ligthert [MVP]

Cor
So you think it is safe to just leave it as is and ignore the warning?

No I think that it is good to investigate the warning, and when you are sure
that it gives no problem, leave it as warning.

My expirience is that there is mostly something not completly right in the
code when this warning occurs, it is very well done.

Cor
 
T

Terry Olsen

How about this then?

Public Function DoesKeyExist() As Boolean
Try
Dim oReg as RegistryKey = m_MSRegRoot.OpenSubKey(m_sKeyPath,False)
If oReg Is Nothing then
Return False
Else
oReg.Close() 'Don't really believe these two line are necessary
oReg = Nothing 'as oReg will go out of scope on exit of the function
Return True
Catch
Return False
End Try
End Function
 
D

david

Terry,


I strongly disagree with you. The only thing what you do with this is
creating extra code and disable the checking on wrong used code.

If you strongly disagree, then what do you suggest? The OP needs to
assign the key in the try block, and needs to reference the key in
the catch or finally in order to close it on error. Without Terry's
explicit assignment, that's a warning in 2005.
 
C

Cor Ligthert [MVP]

David,
If you strongly disagree, then what do you suggest? The OP needs to
assign the key in the try block, and needs to reference the key in
the catch or finally in order to close it on error. Without Terry's
explicit assignment, that's a warning in 2005.
Before you reply read than the other messages in this thread, there are in
my opinion answers enough not alone by me.

Cor
 

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