Link to home
Start Free TrialLog in
Avatar of Cool-Cell
Cool-Cell

asked on

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

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.
ASKER CERTIFIED SOLUTION
Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of Cool-Cell
Cool-Cell

ASKER

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.