473,396 Members | 1,892 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,396 software developers and data experts.

improving my writing - 2005

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?


Nov 21 '05 #1
25 1269
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
Nov 21 '05 #2
When you declare the variable, assign it an initial value...such as:

dim oReg as RegistryKey = Nothing

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #3
:-0

OMG. I use that in most other places.

Why did my brain not associate that with the error message recieved?
Thanks Terry
"Terry Olsen" <to******@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
When you declare the variable, assign it an initial value...such as:

dim oReg as RegistryKey = Nothing

*** Sent via Developersdex http://www.developersdex.com ***

Nov 21 '05 #4

"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
Nov 21 '05 #5
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
Nov 21 '05 #6
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.
--
Dennis in Houston
"Cor Ligthert [MVP]" wrote:

"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

Nov 21 '05 #7
I am confused...

Kaypee wrote:
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 Olsen wrote: 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?

--
Larry Lard
Replies to group please

Nov 21 '05 #8
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
Nov 21 '05 #9
Dennis

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

Cor
Nov 21 '05 #10
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.
--
Dennis in Houston
"Cor Ligthert [MVP]" wrote:

"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

Nov 21 '05 #11
I am confused...

Kaypee wrote:
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 Olsen wrote: 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?

--
Larry Lard
Replies to group please

Nov 21 '05 #12
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
Nov 21 '05 #13
Dennis

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

Cor
Nov 21 '05 #14
Larry

From what I can figure:

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
I am confused...

Kaypee wrote:
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
it is possible that exception may occur in next line - thus nothing ever
assigned to oReg
execution moves onto Catch and then Finally
oReg = m_MSRegRoot.OpenSubKey(m_sKeyPath, False)
If Not oReg Is Nothing Then
bExist = True
End If
Catch ex As Exception
'
Finally
thus it may get here before oReg actually used?
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 Olsen wrote:
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?

--
Larry Lard
Replies to group please

Nov 21 '05 #15
Cor

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

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:Ok*************@TK2MSFTNGP15.phx.gbl...
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

Nov 21 '05 #16
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

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

Nov 21 '05 #17
Larry

From what I can figure:

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
I am confused...

Kaypee wrote:
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
it is possible that exception may occur in next line - thus nothing ever
assigned to oReg
execution moves onto Catch and then Finally
oReg = m_MSRegRoot.OpenSubKey(m_sKeyPath, False)
If Not oReg Is Nothing Then
bExist = True
End If
Catch ex As Exception
'
Finally
thus it may get here before oReg actually used?
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 Olsen wrote:
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?

--
Larry Lard
Replies to group please

Nov 21 '05 #18
> 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
Nov 21 '05 #19
Cor

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

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:Ok*************@TK2MSFTNGP15.phx.gbl...
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

Nov 21 '05 #20
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

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

Nov 21 '05 #21
> 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
Nov 21 '05 #22
On 2005-09-09, Cor Ligthert [MVP] <no************@planet.nl> wrote:
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.


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.
Nov 21 '05 #23
On 2005-09-09, Cor Ligthert [MVP] <no************@planet.nl> wrote:
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.


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.
Nov 21 '05 #24
David,

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.

Before you reply read than the other messages in this thread, there are in
my opinion answers enough not alone by me.

Cor
Nov 21 '05 #25
David,

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.

Before you reply read than the other messages in this thread, there are in
my opinion answers enough not alone by me.

Cor
Nov 21 '05 #26

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

Similar topics

63
by: Stephen Thorne | last post by:
Hi guys, I'm a little worried about the expected disappearance of lambda in python3000. I've had my brain badly broken by functional programming in the past, and I would hate to see things...
6
by: pruebauno | last post by:
I am sure there is a better way of writing this, but how? import re f=file('tlst') tlst=f.read().split('\n') f.close() f=file('plst') sep=re.compile('Identifier "(.*?)"') plst= for elem in...
14
by: Ron Johnson | last post by:
Hi, While on the topic of "need for in-place upgrades", I got to think- ing how the pg_restore could be speeded up. Am I wrong in saying that in the current pg_restore, all of the indexes are...
1
by: Robin | last post by:
For an asp.net project that is deployed to a load balanced web servers, are there any performance changes that can be made in .Net runtime or IIS 6? Also are there any additional tips for...
2
by: Irfan Akram | last post by:
Hi Guys, I am in search of some innovative ideas in improving the interface of Online Marking System. At the moment it has some good functionality, but lacks a professional interface. Is...
0
by: Yunus's Group | last post by:
Yunus's Group May 23, 3:36 pm show options Newsgroups: microsoft.public.dotnet.languages.vb From: "Yunus's Group" <yunusasm...@gmail.com> - Find messages by this author Date: 23 May 2005...
3
by: Kaypee | last post by:
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
1
by: mjheitland | last post by:
Hello, does anyone know if an update is available (or at least planned) for the much cited standard reference IMPROVING .NET APPLICATION PERFORMANCE AND SCALABILITY? link:...
16
by: Dylan Parry | last post by:
Hi, I used SQL Server 2005 on my development machine, and whilst this machine isn't as powerful as the live server, it does at times seem a little slower than I would expect. So I've been...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.