2

I have a class like this

#region Properties
    private static string inputURL;
    public static string InputURL
    {
        get { return inputURL; }
        set { inputURL = value; }
    }
    private static string outputURL;
    private static string ffBaseURL = "format=xml&";
    public static string FFBaseURL
    {
        get { return ffBaseURL; }
        set { ffBaseURL = value; }
    }
    private static string excludeParam = "fullurl,log";
    public static string ExcludeParam
    {
        get { return excludeParam; }
        set { excludeParam = value; }
    }
    private static string currentCategoryID = "234";
    public static string CurrentCategoryID
    {
        get { return currentCategoryID; }
        set { currentCategoryID = value; }
    }
    private static string navigationParameters = "query=*&log=navigation&filterCategoryId=" + currentCategoryID;
    public static string NavigationParameters
    {
        get { return navigationParameters; }
        set { navigationParameters = value; }
    } 
    #endregion

    #region Methods
    public static string NavigationCall()
    {

        List<string> excludeParams = new List<string>(excludeParam.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
        foreach (string key in HttpContext.Current.Request.QueryString.Keys)
        {
            if (!excludeParams.Contains(key))
            {
                FFBaseURL += key + "=" + HttpContext.Current.Request[key] + "&";
            }
        }
        FFBaseURL += NavigationParameters;


        if (Common.IsInternalIP())
        {
            FFBaseURL += "&log=internal";
        }
        outputURL = ffBaseURL;
        return outputURL;
    } 
    #endregion

As you can see I have a static function called NavigationCall() ,it is mandatory that this function remains static.And when I calls this function from my website the function returns wrong values in each function call because of the static properties i declared.We all know static properties will retain their values after the exection of the programe.

So lets say when i call these function first time I gets a result "tesresult1",second time when i reloads my webpage it gives me a result "testresult1testresult1".I think you got the problem now.

  1. I Have tried to solve this issue by declaring static variable values again ,but it does not looks like a good way to programe things.

  2. I tried to make the properties non static .but it returns error as NavigationCall() is a static function i can't call non static properties inside it.

Now I am searching for a correct way to resolve this issue, I think this problem came to me because of the wrong understanding of OOPS concept.Can any one lend a hand here to solve the case or if the issue is vast point to some resources where i can understand how to find a solution?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
None
  • 5,582
  • 21
  • 85
  • 170
  • 1
    It would help if you'd reset (read as "reassign" the base value) your `FFBaseURL` parameter inside your function, but I totally agree with @Szymon's answer. – Andrei V Oct 04 '13 at 07:05

3 Answers3

4

Instead of using static properties, you can pass all the parameters to your static method.

public static string NavigationCall(
     string inputURL,
     string ffBaseURL,
     string excludeParam,
     string currentCategoryID,
     string navigationParameters
)
{
     // the body of your method
}
Szymon
  • 42,577
  • 16
  • 96
  • 114
3

You can also bundled all properties into Custom object and pass it to method. Also you have to make NavigationCall thread safe for any solution. Are static methods thread safe ?

public static string NavigationCall(CustomNavigation objCustomNavigation)

//Custom object.
public class CustomNavigation
{
  public string InputURL {get;set;}
  public string FBaseURL{get;set;}
  public string ExcludeParam{get;set;}
  public string CurrentCategoryID {get;set;}
  public string NavigationParameters{get;set;}
}
Community
  • 1
  • 1
mit
  • 1,763
  • 4
  • 16
  • 27
  • 1
    I would prefer this over @Szymon's suggestion, because here you have encapsulated the concept of the call in a nice [parameter object](http://sourcemaking.com/refactoring/introduce-parameter-object) in which you can later implement further logic like basic validation, default values... You might want to make the setters private and set the values through the constructor for improved clarity and safety. – EagleBeak Oct 04 '13 at 07:58
  • @EagleBeak It would be great if you could post this as an answer with suitable example – None Oct 04 '13 at 09:01
2

I'd suggest to introduce a parameter object (as @mit suggested) and use the opportunity to encapsulate some of your logic there. This should instantly simplify your method. Maybe you could then make some of these properties private, because they'll only be needed in logic encapsulated in the parameter object.

    //Your static method
    public static string NavigationCall(CustomNavigation customNavigation)

    //Disclaimer: I have no idea, whether this is an appropriate name. 
    //It really depends on what you want to do with his class
    class CustomNavigation
    {
        public string InputURL { get; private set; }
        public string FFBaseURL { get; private set; }
        public IEnumerable<string> ExcludeParams { get; private set; }
        public string CurrentCategoryID { get; private set; }
        public string NavigationParameters { get; private set; }

        public CustomNavigation(string inputUrl, string excludeParam, string fBaseUrl, string currentCategoryID, string navigationParameters)
        {
            // various guard clauses here...

            NavigationParameters = navigationParameters;
            CurrentCategoryID = currentCategoryID;
            FFBaseURL = fBaseUrl;
            InputURL = inputUrl;

            // Parse string here -> Makes your method simpler
            ExcludeParams = new List<string>(excludeParam.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
        }

        //Example for encapsulating logic in param object
        public void AddKeys(HttpContext currentContext)
        {
            var keys = currentContext.Request.QueryString.Keys
                        .Cast<string>()
                        .Where(key => !ExcludeParams.Contains(key));

            foreach (var key in keys)
                FFBaseURL += key + "=" + currentContext.Request[key] + "&";
        }
    }
EagleBeak
  • 6,939
  • 8
  • 31
  • 47