Solved

Get Visual Studio .NET 2010 to stop reporting a Error or show me how to fix the error

Posted on 2011-09-22
3
239 Views
Last Modified: 2013-11-27
I'm learning just started learning Visual Studio .NET 2010 a big step from VB6 how do I get VS 2010 to stop reporting a Error or show me how to fix the error so the IDE will stop bugging me about it.

Here is the code:

Function GetMSAppShortPath(ByVal exeCode As Integer) As String

        Dim exeName As String = ""
        If exeCode = 1 Then exeName = "Msaccess.exe"
        If exeCode = 2 Then exeName = "Winword.exe"
        If exeCode = 3 Then exeName = "Excel.exe"
        If exeCode = 4 Then exeName = "Outlook.exe"
        If exeCode = 5 Then exeName = "IExplore.exe"
        If exeCode = 6 Then exeName = "PowerPnt.exe"

        Dim regKey As Microsoft.Win32.RegistryKey
        Dim strPath As String
        Try

            regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey( _
                "Software\Microsoft\Windows\CurrentVersion\App Paths\" & exeName)
            strPath = regKey.GetValue("")

        Catch e As Exception
            strPath = ""
        Finally

            If Not regKey Is Nothing Then regKey.Close()
        End Try
        Return strPath

    End Function


The error popping up:
Is the first regkey value in the below code line. Sure the answer is simple but still have to learn how to fix it. Thank You
If Not regKeyIs Nothing Then regKey.Close()

Error Window Box Display for this error:      
Variable 'regKey' is used before it has been assigned a value. A null reference exception could result at runtime.
0
Comment
Question by:Cool-Cell
3 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 36584063
Just change your definition of the regKey variable in the following line so it is set to nothing should get rid of the error :

Dim regKey As Microsoft.Win32.RegistryKey = Nothing

0
 
LVL 3

Expert Comment

by:libby9284
ID: 36584066
I'm pretty sure the only way to get rid of the error message is to resolve it.  Why do you have it in a Try/Catch block?  Try using an if/else block instead, testing regKey for value Nothing after its assigned.
0
 
LVL 1

Author Closing Comment

by:Cool-Cell
ID: 36584122
Thank you! The solution was simple like I thought and helped in my learning process.

Answer the Question to other response:
Why in Try - Catch statement because just learning from source code I can find on the internet and reading EE answered question and asking questions on EE if I get stumped.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question