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
242 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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

690 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