Difficulties in using static functions and variables in C #

advertisements

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?


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;}
}