473,757 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A Generic Generics Problem

Hello everyone,
I'm writing because I'm frustrated with the implementation of C#'s
generics, and need a workaround. I come from a Java background, and
am currently writing a portion of an application that needs
implementations in both Java and C#. I have the Java side done, and
it works fantastic, and the C# side is nearly there. The problem I'm
running into has to do with the differences in implementations of
Generics between the two languages. I would like to use a class that
is defined with Generics, only without having to declare its type
everywhere.

For example, suppose I'm using an IList, and in one location, I
declare it using a generic type of <String>. Now, in a different
class, I'd like to store that list as a field, but I don't want to
store the type associated with it. If I'm required to store the type
with the field (such as <pre>private IList<Stringfie ldName;</pre>,
then the class in which it is stored will be required to be generic,
and any classes that store instances of it will also need to be
generic, ad infinitum, until all my code is generified all over the
place (which I don't want!). Now, the example presented here is
easily solvable, since the IList interface is declared twice (once in
System.Collecti ons as a non-generic version, and once in
System.Collecti ons.Generic as a generic version).

Here's a code example of the Java equivalent, followed by the
(illegal) desired C# syntax:
<pre>
public interface ExampleInterfac e<Type>
{
// some member methods in here
}

public class ExampleImpl<Typ eimplements ExampleInterfac e<Type>
{
// method implementations here
}

public class SampleClass
{
private ExampleInterfac e mExampleInterfa ce;

public void setExampleInter face(ExampleInt erface
pExampleInterfa ce)
{
mExampleInterfa ce = pExampleInterfa ce;
}
}

public class SampleBuilder
{
private SampleClass mSampleClass;

public SampleBuilder()
{
mSampleClass = new SampleClass();
mSampleClass.se tExampleInterfa ce(new ExampleImpl<Str ing>());
}
}

// end Java example
</pre>

Desired C# implementation:
<pre>
public interface ExampleInterfac e<Type>
{
// some member methods in here
}

public class ExampleImpl<Typ e: ExampleInterfac e<Type>
{
// method implementations here
}

public class SampleClass
{
private ExampleInterfac e exampleInterfac e;

public void SetExampleInter face(ExampleInt erface exampleInterfac e)
{
this.exampleInt erface = exampleInterfac e;
}
}

public class SampleBuilder
{
private SampleClass sampleClass;

public SampleBuilder()
{
this.sampleClas s = new SampleClass();
this.sampleClas s.SetExampleInt erface(new
ExampleImpl<Str ing>());
}
}

// end C# example
</pre>

So, in the C# example, if I'm forced to store the instance of
<pre>ExampleInt erface</prewith its generic type, then
<pre>SampleClas s</prewill have to be defined as a Generic class, and
stored in SampleBuilder with its generic type, which will then force
<pre>SampleClas s</preto also be defined as a Generic class, and so
on and so forth.

Is there any way to do what I'd like, or am I forced to abandon the
use of Generics completely in C#?

Thanks in advance!
Respectfully,
Robert Kausch

May 17 '07
13 3836
On May 17, 2:46 pm, "rkau...@gmail. com" <rkau...@gmail. comwrote:
On May 17, 1:10 pm, "rkau...@gmail. com" <rkau...@gmail. comwrote:
<snip>

Whoops... looks like a little bit of Chat information got in there...
sorry about that.

May 17 '07 #11
rk*****@gmail.c om <rk*****@gmail. comwrote:

<snip>
Column.cs contains an accessor, and that accessor is Generic in Java
(though stored without being generic), and all data is read from that
accessor. Though, in C#, the language forces you into storing the
accessor with its type.
Only if you want to use the generic type as the variable type. Here's
an example of not doing so:

IList list = new List<string>();

This is still type-safe - I can't add any non-string references to the
list - but the type safety is now only at execution time, not at
compile-time.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 17 '07 #12
On May 17, 3:05 pm, Jon Skeet [C# MVP] <s...@pobox.com wrote:
rkau...@gmail.c om <rkau...@gmail. comwrote:

<snip>
Column.cs contains an accessor, and that accessor is Generic in Java
(though stored without being generic), and all data is read from that
accessor. Though, in C#, the language forces you into storing the
accessor with its type.

Only if you want to use the generic type as the variable type. Here's
an example of not doing so:

IList list = new List<string>();

This is still type-safe - I can't add any non-string references to the
list - but the type safety is now only at execution time, not at
compile-time.

--
Jon Skeet - <s...@pobox.com >http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jon-

Thanks for the reply, I think we might be getting closer to closing
the gap that I'm trying to bridge... Now, Ludwig earlier suggested
that we use a non-generic base class to do this. As I explained to
him earlier, I still need the methods defined in the interface
(whether they're defined in the Generic or Non-Generic sense) in the
Column class, and again, I'd prefer not to make Column generic.

So, here's basically the guts of what I'm trying to do:
public class Column
{
private ColumnAccessor accessor;

public Object GetValue()
{
return accessor.GetVal ue();
}
}

public interface IColumnAccessor <Type>
{
Type GetValue();
}

public class ColumnAccessor< Type: IColumnAccessor <Type>
{
public Type GetValue()
{
// get the value here from elsewhere.
}
}

// now, if we follow Ludwig's example (and indeed, C#'s Collection
framework with IList & IList<T>),
// do we then define:
public IColumnAccessor
{
Object GetValue();
}

public IColumnAccessor <Type: IColumnAccessor
{
Type GetValue();
}

Is there a huge flaw in logic that I'm missing here? Any suggestions
on an alternate way to tackle this?

Thanks again!
Robert

May 17 '07 #13
rk*****@gmail.c om <rk*****@gmail. comwrote:

<snip>
Is there a huge flaw in logic that I'm missing here? Any suggestions
on an alternate way to tackle this?
You haven't implemented IColumnAccessor in ColumnAccessor< Type(you
should avoid using real type names as type parameters, by the way).
Interfaces can't be implemented with covariant return types.

You need something like:

public class ColumnAccessor< T: IColumnAccessor <T>
{
public T GetValue()
{
// get the value here from elsewhere.
}

object IColumnAccessor .GetValue()
{
return GetValue();
}
}
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 17 '07 #14

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

Similar topics

49
2893
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another class). Any comments would be appreciated! Thanks! Steve ----------------------------------------------------------------------
17
3325
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the way they are. ***** Summary & Questions ***** In a nutshell, the current .NET generics & .NET framework make it sometimes difficult or even impossible to write truly generic code. For example, it seems to be impossible to write a truly generic
10
2318
by: steve bull | last post by:
I have a class SwatchPanel which takes Swatch as a parameter type. How can I call a static function within the Swatch class? For example the code below fails on TSwatch.Exists. How can I get the call to work? Exists is a method within the Swatch class NOT the SwatchPanel class. Is this possible? Suggestions would be very welcome.
9
12849
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that implements IQueue<Tfor all types T (so it can make use of queues of various object types internally). As useful as this is, it doesn't seem possible. The natural (but illegal) notation would be something like class A<QueueClasswhere QueueClass :...
1
3430
by: interX | last post by:
Hi I'm new in VC++ and have a question to generics. I have a generic class, which contains an array of the generic type. This array I can pin and then I would like to get an unmanaged pointer to it. Therefore I wanted to creat a class member which represents the pointer to the array. Unfortunately I get the folowring compile error for the code beneath: error C3229: 'DataType *' : indirections on a generic type parameter
9
5849
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context, culture, InValue);
10
1938
by: Egghead | last post by:
Hi all, Can someone kindly enough point me to some situations that we shall or "must" use Generic Class? I can foresee the Generic Method is powerful, but I can not find a single situation that I will need the Generic Class, given there are so many other options. -- cheers,
10
2713
by: phancey | last post by:
I'm quite new to generics. I have 2 generic classes: MyClass<Tand MyOtherClass<T>. MyClass<Thas 2 public Add methods Add(MyOtherClass<T>); Add(MyOtherClass<Wrapper<T>>); (Wrapper<Tis another class that contains an object of type T) I have a third generic class: MyHandler<Twhich contains 2 Add methods and a generic method: Add(T obj)
11
2551
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use generics instead of System.Object. I want the code in Form1_Load to remain exactly the same, but in the background I want to use generics. I'm trying to get a better understanding of how it works and I'm a little stuck.
0
9487
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9904
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9884
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9735
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.