Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning To Start of Method / Function

Status
Not open for further replies.

danielkelly

IS-IT--Management
Aug 13, 2006
28
AU
Hi,

Im having some difficulties understanding how best to get around my problem. Basically inside a method I am checking for the existance of various Registry Keys to ensure the correct structure is in place. The actual Method below is called CheckRegistryKeys().

Here is a code Snippet :-

private void CheckRegistryKeys()
{

RegistryKey hkcu = Registry.LocalMachine;

try
{

//Opens the Required Registry Keys
RegistryKey hkSoftware = hkcu.OpenSubKey("Software", true);
RegistryKey hkGC = hkSoftware.OpenSubKey("GC", true);

//Check if the GC Key Exists
if (hkGC == null)
{
//The Key does not exist. This is the First Time Running the Program so Create the Default Registry Keys
RegistryKey hkNewGC = hkSoftware.CreateSubKey("GC");

//Now Create The Default Registry Keys and Values
RegistryKey hkNewDatabase = hkNewGC.CreateSubKey("Database");

//Show The Database Location Screen
frmDatabaseSettings formDatabase = new frmDatabaseSettings();
formDatabase.ShowDialog();

//Restart The Method to load the new values
CheckRegistryKeys();
}

//Open The Database Sub Key
RegistryKey hkDatabase = hkGrindCash.OpenSubKey("Database",true);

//Check if the Database Sub Key Exists
if (hkDatabase == null)
{
//The Key does not Exist so Create it
RegistryKey hkDatabaseNew = hkGC.CreateSubKey("Database");

//Show The Database Location Screen
frmDatabaseSettings formDatabase = new frmDatabaseSettings();
formDatabase.ShowDialog();

//Restart The Method to load the new values
CheckRegistryKeys();
}

//Get The Values From The Registry and Check That They Are Not Empty
string strDatabaseLocation = Convert.ToString( hkDatabase.GetValue("Server"));
string strUserName = Convert.ToString(hkDatabase.GetValue("Username"));
string strPassword = Convert.ToString(hkDatabase.GetValue("Password"));

}
catch (Exception exc)
{
mySQLFunctions.LogError(clsGlobalVariables.GlobalUserName, "frmSplashScreen", "CheckRegistryKeys", "An Error Occurred Whilst Checking The Registry Keys", exc.Message.ToString());
}

}

I thought this was a suitable way to do it and it does work however I always get an Object Not Set To Instance Error which my Error handler is handling when I have no Registry Keys (initially).

Debugging the App shows me that restarting the Method (CheckRegistryKeys()) is where it is falling over as once it passes the first restart, it moves to the second and when it finished the second it returns to the first probably because Im assuming that is where the operation branched off. It then throws and Object Instance Exception. I hope this makes sense.

Can anyone suggest a better way to do this or offer some assistance.

Thanks,

Daniel.
 
Rather than have just one big try/catch block, break the process down in each part... if part #1 fails and it's okay, but you still want to do part #2, do something like:

try {
try {
PART #1
}
catch (expected exception types) {
HANDLE ERROR(s)
}
try {
PART #2
}
catch (expected exception types) {
HANDLE ERROR(s)
}
}
catch {
}
 
In your method you are recursively calling yourself which is fine but you need to break out after the recursive call. Otherwise after the recursion the execution of the method will continue where it left off.

Code:
try
{
    RegistryKey hkSoftware = hkcu.OpenSubKey("Software", true);
    RegistryKey hkGC = hkSoftware.OpenSubKey("GC", true);
        
    if (hkGC == null)
    {
        RegistryKey hkNewGC = hkSoftware.CreateSubKey("GC");

        RegistryKey hkNewDatabase = hkNewGC.CreateSubKey("Database");
                
        frmDatabaseSettings formDatabase = new frmDatabaseSettings();
        formDatabase.ShowDialog();
                    
        CheckRegistryKeys();
        [b][COLOR=red]return;[/color][/b]  // Stop here or below code will still be executed after the recursion.
    }

    RegistryKey hkDatabase = hkGC.OpenSubKey("Database",true);

    if (hkDatabase == null)
    {
        RegistryKey hkDatabaseNew = hkGC.CreateSubKey("Database");

        frmDatabaseSettings formDatabase = new frmDatabaseSettings();
        formDatabase.ShowDialog();
                    
        CheckRegistryKeys();
        [b][COLOR=red]return;[/color][/b]  // Stop here or below code will still be executed after the recursion.
    }

    string strDatabaseLocation = Convert.ToString(hkDatabase.GetValue("Server"));
    string strUserName = Convert.ToString(hkDatabase.GetValue("Username"));
    string strPassword = Convert.ToString(hkDatabase.GetValue("Password"));               
}
catch (Exception exc)
{
    mySQLFunctions.LogError(clsGlobalVariables.GlobalUserName, "frmSplashScreen", "CheckRegistryKeys", "An Error Occurred Whilst Checking The Registry Keys", exc.Message.ToString());
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top