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|improve this question
add comment

1 Answer

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|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.