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
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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.

733 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