it seems to me that the implementation of generics allows nothing but the most trivial of exercises to be done - without a means of
doing anything different based on the type allows only duplication and little variation.
For example if I want to write
public class SwatchPanel<TSwatch> : System.Windows.Forms.Panel where TSwatch : Swatch
{
List<TSwatch> swatchList;
public MethodA
{
Swatch.Write(swatchList);
}
}
to have different methods for writing out the swatches based on type. the compiler does not appear to let me.
I cannot say TSwatch.Write and when I write the following under the Swatch class
public class Swatch
{
public static void Write(List<Swatch> colorSwatchList)
{
write to swatch file
}
public static void Write(List<ColorSwatch> colorSwatchList)
{
write to color swatch file
}
public static void Write(List<ShadeSwatch> shadeSwatchList)
{
write to shade swatch file
}
}
the compiler tells me
Can't convert from 'System.Collections.Generic.List<TSwatch>' to 'System.Collections.Generic.List<NewControls.Swatc h>'
it always picks up the 1st override. If I switch the Swatch override with the ColorSwatch override it will want to convert to a
ColorSwatch generic list.
Maybe I am doing something very stupid here but it appears to be very difficult to do anything of any use with generics except
cookie cutting. They are a brain dead implementation.
If I make SwatchPanel a normal class and derive classes from it I can do everything all of the above w/o problem. Then the problem
is with making the Swatch.Write work with List<Swatch> swatchList. Each child class then has to convert from List<Swatch> to
List<ColorSwatch> etc when it wants to do the actual write in the method appropriate to it.
I am sorry if this is not the place to bring this up. But the implementation seems pretty poorly thought out.
Thanks,
Steve
On Thu, 8 Dec 2005 01:56:38 -0000, Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
steve bull <bu****@comcast.net> wrote: I have a class SwatchPanel which takes Swatch as a parameter type.
How can I call a static function within the Swatch class?
Why not just call Swatch.Exists, i.e. don't use the generic argument at
all? You won't be able to use an "overridden" version of Swatch.Foo,
but then you can't override static methods anyway.