Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm doing a simple overloading of a function and I create a SPSite object when I'm calling the second function. I'm not sure of how to dispose of it in this scenario.

public static string GetConfigValue(string name, string webURL)
{
   return GetConfigValue(configName, new SPSite(webURL).RootWeb);
}
share

1 Answer 1

up vote 6 down vote accepted

Like this :

public static string GetConfigValue(string configName, string webURL)
{
   string returnVal = null;

   using (SPSite site = new SPSite(webURL)) {
      returnVal = GetConfigValue(configName, site.RootWeb);
   }

   return returnVal;
}

That assumes that you have another overloaded version of GetConfigValue(string, SPWeb).

You should be aware that instantiating SPSite and SPWeb objects in utility functions is a horribly inefficient way to code and will perform poorly under stress.

share

This site is currently not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .