views:

59

answers:

2

hi
this code works fine and my config file changes correctly.

    //Local Variable Declaration
System.Configuration.Configuration oConfig =
    System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(
    Request.ApplicationPath);

if (oConfig .AppSettings.Settings["CompanyName"] == null)
{
    oConfig AppSettings.Settings.Add("CompanyName", "MyCompanyName");
    oConfig .Save();
}

but when I want to use a property for this purpose Nothing happend in Config File.

// Property Declaration

private System.Configuration.Configuration _oRootConfig;

public System.Configuration.Configuration oRootConfig
{
   get
   {
       return
           System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(
           Request.ApplicationPath);           

   }
   set { _oRootConfig = value; }
}

if (oRootConfig.AppSettings.Settings["CompanyName"] == null)
{
   oRootConfig.AppSettings.Settings.Add("CompanyName", "MyCompanyName");
   oRootConfig.Save(System.Configuration.ConfigurationSaveMode.Modified, true);
}

now i have two question:
1-why this code doesnot work ,and there is no error.
2-if i want to programn in object oriented manner ,what can i do to fix this property if the problem is related to the property. thanks

+1  A: 

this line of code:

get 
{ 
return  (System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)); 
}

set { _oRootConfig = value; }

you are not setting _oRootConfig in your get. You need this code:

get
{
     _oRootConfig = (System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath));
     return _oRootConfig;
}

set
{
     _oRootConfig = value;
}
Richard J. Ross III
+2  A: 

You're reopening the config on every get, do this instead:

get
{
    if(this._oRootConfig == null)
        this._oRootConfig = (System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath));
    return this._oRootConfig;
}
MStodd
Yep, I missed that...good point.
Richard J. Ross III
hi man you re great ,your solution works fine ,could you explain more,is this a pattern what i was missing exactly?thanks
siamak
The problem is that you don't understand what a property actually is. It's simply syntactic sugar, a nicety of the language. All that happens when a property is compiled is the getter gets turned into a function called get<property_name> and the setter gets turned into a function called set<property_name> (i think). Do some googling, but just remember to treat the getter and setter like functions because their code will get run every time they're called.
MStodd
@Siamak - Your problem was that oRootConfig's getter, each time it was called, was re-opening the config file. So, each reference to oRootConfig was generating a new instance of the object, and the old one then goes out of scope. Your code was thus creating three different instances of the configuration object; one to check AppSettings, one to add the new AppSetting and another to save the file. Each instantiation "blew away" the prior instance, along with any changes you had made.
KeithS
thank u ,todd ,i think that i know about the nature of the property but Keith explanation satisfy me ,thank you every one
siamak
no prob, don't forget to mark the correct answer 'accepted' ;-)
MStodd
just let me say somthing I think that every begginer (like me )must respects experts , and we could learn more and more every day from each other ,that s why i love The Programming world and Its guys with all of the difficulties are involoved ,so ,I hope to see u guys in next issues -)
siamak