C
Charles Churchill
I apologize if this question has been asked before, but after about
half an hour of searching I haven't been able to find an answer online.
My code is beloiw, with comments pertaining to my question
In short my question is why when I pass a generic type directly to the
formatObject function it works fine, but when I pass it to the
checkText function where it is itself a generic argument, and then it
is passed to formatObject, it is seen as an Object and not as the type
it is? Any help would be greatly appreciated.
Here are the relevant sections of my code (I apologize for any typos as
the actual code is on a remote system that I cannot cut and past from)
public class FormatObject
{
public static string formatObject(Object o)
{
return o.ToString();
}
public static string formatObject<K,V>(KeyValuePair<K,V> kvpo)
{
//do some reflectiony sort of things to build up string
// that looks like (KeyValuePair<Int32, Int32> = {10,10})
message += formatObject(kvpo.Key);
// call formatObject on the key and value
// recursively in case they are of a type handled
// by other formatObject calls
return message
}
// a few other overridden formatObject functions taking different
// types (List<T>, List<List<T>>, etc)
}
public class CheckText
{
public bool void checkText(T t, string matchPattern)
{
string ostr;
ostr = FormatObject.formatObject(t);
return (ostr.IndexOf(matchPattern) != -1)
}
}
public void Main(string[] args)
{
KeyValuePair<int, int> k = new KeyValuePair<int, int>(10,.10);
string formatted = FormatObject.formatObject(k);
// Calls the appropriate function and returns a string consisting of
// (KeyValuePair<Int32, Int32> = {10,10})
bool doesContain = CheckText.checkText(k,"(KeyValuePair");
// returns false as it calls the Object overriden version
// of formatObject. which returns a string
// consisting of "{10,10}"
}
half an hour of searching I haven't been able to find an answer online.
My code is beloiw, with comments pertaining to my question
In short my question is why when I pass a generic type directly to the
formatObject function it works fine, but when I pass it to the
checkText function where it is itself a generic argument, and then it
is passed to formatObject, it is seen as an Object and not as the type
it is? Any help would be greatly appreciated.
Here are the relevant sections of my code (I apologize for any typos as
the actual code is on a remote system that I cannot cut and past from)
public class FormatObject
{
public static string formatObject(Object o)
{
return o.ToString();
}
public static string formatObject<K,V>(KeyValuePair<K,V> kvpo)
{
//do some reflectiony sort of things to build up string
// that looks like (KeyValuePair<Int32, Int32> = {10,10})
message += formatObject(kvpo.Key);
// call formatObject on the key and value
// recursively in case they are of a type handled
// by other formatObject calls
return message
}
// a few other overridden formatObject functions taking different
// types (List<T>, List<List<T>>, etc)
}
public class CheckText
{
public bool void checkText(T t, string matchPattern)
{
string ostr;
ostr = FormatObject.formatObject(t);
return (ostr.IndexOf(matchPattern) != -1)
}
}
public void Main(string[] args)
{
KeyValuePair<int, int> k = new KeyValuePair<int, int>(10,.10);
string formatted = FormatObject.formatObject(k);
// Calls the appropriate function and returns a string consisting of
// (KeyValuePair<Int32, Int32> = {10,10})
bool doesContain = CheckText.checkText(k,"(KeyValuePair");
// returns false as it calls the Object overriden version
// of formatObject. which returns a string
// consisting of "{10,10}"
}