I have two function that one receives a FormCollection and another receives a HttpRequest like this:
public void SetUrlParameters(FormCollection request, string controllerName = "")
{
string sessionID = ConvertToString(request["sessionID"]);
string idSession = ConvertToString(request["idSession"]);
string sessionid = ConvertToString(request["sessionid"]);
if (idSession.Length > 0)
this.sessionID = idSession;
else if (sessionid.Length > 0)
this.sessionID = sessionid;
else
this.sessionID = sessionID;
}
private void SetUrlParameters(HttpRequest request, string controllerName = "")
{
string sessionID = ConvertToString(request["sessionID"]);
string idSession = ConvertToString(request["idSession"]);
string sessionid = ConvertToString(request["sessionid"]);
if (idSession.Length > 0)
this.sessionID = idSession;
else if (sessionid.Length > 0)
this.sessionID = sessionid;
else
this.sessionID = sessionID;
}
private string ConvertToString(object obj, string defaultValue = "")
{
if (obj == null) return defaultValue;
return Convert.ToString(filterSameVariablesValue(obj));
}
As you can see, the both the functions do exact the same. The only difference is the type of the value that each one receives.
Both function are working. What I want is to avoid having repeated code in both function.
There is any way that I can collapse this to function into one ? Like for example convert the FormCollection and HttpRequest to a Hashtable and the use it?