473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

passing a generic type to a generic class

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,Vkvpo)
{
//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, intk = 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}"
}

Aug 24 '06 #1
4 3119
Charles,

Can you please post compilable sample that demonstrates the problem -
console application is preferable.

--
Stoitcho Goutsev (100)

"Charles Churchill" <gy******@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
>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,Vkvpo)
{
//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, intk = 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}"
}

Aug 24 '06 #2
Stoitcho Goutsev (100) wrote:
Charles,

Can you please post compilable sample that demonstrates the problem -
console application is preferable.

--
Stoitcho Goutsev (100)
Here you go:

namespace TestProgram
{
public class FormatObject
{
public static string formatObject(Object o)
{
return o.ToString();
}

public static string formatObject<K, V>(KeyValuePair<K, Vkvpo)
{
return "KeyValuePair<" + typeof(K).Name + ","
+ typeof(V).Name + "= " + kvpo.ToString();
}
}

public class CheckText
{
public static bool checkText<T>(T t, string matchThis)
{
string ostr = FormatObject.formatObject(t);
return (ostr.IndexOf(matchThis) != -1);
}

}

class Program
{
static void Main(string[] args)
{
KeyValuePair<int, intk = new
KeyValuePair<int,int>(500,2000);
Console.WriteLine(FormatObject.formatObject(k));
bool found = CheckText.checkText(k, "KeyValuePair");
Console.ReadLine();
}
}
}

Thanks

Aug 24 '06 #3

Charles Churchill wrote:
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,Vkvpo)
{
//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, intk = 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}"
}
I'm not using generics yet, but a couple of minutes looking at your
code leads me to believe that your problem has nothing to do with
generics, as such. Rather, it involves the following:

"Which overload of a method should be called is determined at compile
time, not run time. Method overRIDES are resolved at run time; method
overLOADS are resolved at compile time."

So, in your case, the line

ostr = FormatObject.formatObject(t);

will result in a call to FormatObject.formatObject(Object o) unless T
is a subtype of KeyValuePair<K, V>. Even if at run time the variable t
refers to an instance of a class that implements the correct interface
/ inherits from the correct class so that a call to
FormatObject.formatObject<K, Vwould be possible, it still won't
happen because the compiler has no way of knowing that that will be the
case.

Take a simpler, non-generic example:

public class Foo : IComparable
{
public int CompareTo(object other)
{
// Native Foo object greater than any other kind of object
return 1;
}

public int CompareTo(Foo otherFoo)
{
... do some stuff to compare Foos ...
}
}

public class Other
{
public static int CompareFoo(Foo aFoo, object something)
{
return aFoo.CompareTo(something);
}
}

A call such as myOther.CompareToFoo(...) will _always_ return -1. Why?
Because no matter what is passed as the argument "something", the
compiler has already determined that the correct signature to call is
Foo.CompareTo(object), based on the fact that the "something" argument
is declared as an object type. Even if you pass a Foo instance at run
time, it still calls the Foo.CompareTo(object) method.

The usual way around this is to do the testing yourself, at run time,
to determine whether the more specific method is appropriate:

public int CompareTo(object other)
{
Foo otherFoo = other as Foo;
if (otherFoo != null)
{
return this.CompareTo(otherFoo);
}
else
{
// Native Foo object greater than any other kind of object
return 1;
}
}

.... or something like that.

In your case, you need to test, within formatObject(object o) whether
"o" is an instance of something that could be passed to the generic
method, and then call the generic method if appropriate.

Maybe. As I said, I'm not on V2.0 yet. :-)

Aug 24 '06 #4
Charles,

Here is the problem - when you call the method
public static bool checkText<T>(T t, string matchThis)
inside the method the only thing that the compiler knows about the type T is
that it has derived from Object. That's why it will treat the *t* as an
object of type System.Object. It won't even consider the generic
formatObject overload since it cannot infer the types of V and K from the
method's type-argument list. You can easily check this by commenting out the
non-generic formatObject. The compiler will report an error.

Normally one gives hints to the compiler constraining types in the
type-argument list.
Unfortunately in your case I don't think the problem can be solved using
constraints.

My solution to the problem is to use one special overload of the checkTest
method that will provide the compiler with the information it lacks at the
moment.

public static bool checkText<K, V>(KeyValuePair<K, Vt, string matchThis)
{
string ostr = FormatObject.formatObject(t);
return (ostr.IndexOf(matchThis) != -1);
}

Using this overload the compiler will be able to infer correctly K and V,
thus call generic overload of formatObject.

I also don't see the point of using generic method for the other overload of
that method. It is equivalent of having Object as the parameter type.

Here is your sample reworked:

namespace TestProgram
{
public class FormatObject
{
public static string formatObject(Object o)
{
return o.ToString();
}

public static string formatObject<K, V>(KeyValuePair<K, Vkvpo)
{
return "KeyValuePair<" + typeof(K).Name + ","
+ typeof(V).Name + "= " + kvpo.ToString();
}
}

public class CheckText
{
public static bool checkText(object t, string matchThis)
{
string ostr = FormatObject.formatObject(t);
return (ostr.IndexOf(matchThis) != -1);
}

public static bool checkText<K, V>(KeyValuePair<K, Vt, string
matchThis)
{
string ostr = FormatObject.formatObject(t);
return (ostr.IndexOf(matchThis) != -1);
}

}

class Program
{
static void Main(string[] args)
{
KeyValuePair<int, intk = new
KeyValuePair<int, int>(500, 2000);
Console.WriteLine(FormatObject.formatObject("100") );
Console.WriteLine(FormatObject.formatObject(k));
bool found = CheckText.checkText("KeyValuePair100", "KeyValuePair");
found = CheckText.checkText(k, "KeyValuePair");
Console.ReadLine();
}
}
}
--
HTH
Stoitcho Goutsev (100)

"Charles Churchill" <gy******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Stoitcho Goutsev (100) wrote:
>Charles,

Can you please post compilable sample that demonstrates the problem -
console application is preferable.

--
Stoitcho Goutsev (100)

Here you go:

namespace TestProgram
{
public class FormatObject
{
public static string formatObject(Object o)
{
return o.ToString();
}

public static string formatObject<K, V>(KeyValuePair<K, Vkvpo)
{
return "KeyValuePair<" + typeof(K).Name + ","
+ typeof(V).Name + "= " + kvpo.ToString();
}
}

public class CheckText
{
public static bool checkText<T>(T t, string matchThis)
{
string ostr = FormatObject.formatObject(t);
return (ostr.IndexOf(matchThis) != -1);
}

}

class Program
{
static void Main(string[] args)
{
KeyValuePair<int, intk = new
KeyValuePair<int,int>(500,2000);
Console.WriteLine(FormatObject.formatObject(k));
bool found = CheckText.checkText(k, "KeyValuePair");
Console.ReadLine();
}
}
}

Thanks

Aug 25 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
2
by: Jeff Bush | last post by:
I am trying to create a generic Command object (following the Command design pattern) that allows me to specify a generic type, an object to operate on, and most importantly, a public Property on...
7
by: Tim | last post by:
When there is a need to pass some dynamic information between 2 managed assemblies, the "Dictionary object" in Generic form can be used as a method parameter to pass the information. The...
1
by: A. Burch | last post by:
What is wrong with this type of generic... I get this error.... Error 1 Inconsistent accessibility: parameter type 'System.Collections.Generic.Queue<TMQ.TheRec>' is less accessible than...
1
by: Néstor Sánchez A. | last post by:
Hi, is there a way, maybe using reflection, to use a generic class passing the type parameter dynamicly (not kwnowing the exact type at compile time)? I tried the next example, but doesn't work: ...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
8
by: MMAS | last post by:
Hey everyone -- Curious about some strange behaviour I'm seeing that seems to be related to my lack of understanding on how generics work in C#. Here's some simplified code (sorry for strange...
2
by: Andrus | last post by:
Winforms UI assembly has static FormManager.FormCreator method which creates forms taking entity as parameter. I need to pass this method to business objects in business assembly so that business...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.