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.
Thanks,
Steve
public class SwatchPanel<TSw atch> : System.Windows. Forms.Panel where TSwatch : Swatch
{
protected virtual bool SetOkButtonStat e()
{
if (TSwatch.Exists (ColorDescripti on))
do something
}
} 10 2318
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.
--
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
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<TSw atch> : System.Windows. Forms.Panel where TSwatch : Swatch
{
List<TSwatch> swatchList;
public MethodA
{
Swatch.Write(sw atchList);
}
}
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<Swat ch> colorSwatchList )
{
write to swatch file
}
public static void Write(List<Colo rSwatch> colorSwatchList )
{
write to color swatch file
}
public static void Write(List<Shad eSwatch> shadeSwatchList )
{
write to shade swatch file
}
}
the compiler tells me
Can't convert from 'System.Collect ions.Generic.Li st<TSwatch>' to 'System.Collect ions.Generic.Li st<NewControls. Swatch>'
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<ColorSwatc h> 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.co m> 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.
steve bull <bu****@comcast .net> wrote: 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.
That's not true at all. It doesn't allow the kind of templating C++
does, but frankly I'm glad of that.
For example if I want to write
public class SwatchPanel<TSw atch> : System.Windows. Forms.Panel where TSwatch : Swatch { List<TSwatch> swatchList;
public MethodA { Swatch.Write(sw atchList); } }
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
<snip>
Indeed. And that would be a bad way of writing it, IMO. It looks like
you want polymorphism at that stage - either have a class which
encapsulates a list of Swatches (generically) and override the
behaviour appropriately, or have one Write method which uses
polymorphism on the Swatch class to work out how to write each entry.
Alternatively, have an instance method in SwatchPanel which writes its
list out.
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.
Cookie-cutting is a good example of exactly what generics are for. The
way you're trying to do things isn't how they're intended to be used,
but that doesn't mean generics are bad. What you're trying to do breaks
object orientation in various ways - for instance, by having a set of
static methods in the base class, you're including knowledge of each of
the subclasses into Swatch into the that base class, when that
knowledge should either be in the subclasses themselves, or at worst
the specialised subclasses of SwatchPanel.
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<ColorSwatc h> 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.
There are limitations which are genuinely problematic, but I don't
think I'd agree that anything you've brought up so far is actually a
problem with generics.
--
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
Jon wrote: steve bull <bu****@comcast .net> wrote: 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. That's not true at all. It doesn't allow the kind of templating C++ does, but frankly I'm glad of that.
I must say I agree with Steve. Generics seem to be useful for writing
typesafe containers, avoiding casts, and not much else. They'd be
capable of so much more if only we had better constraints. If he could
write, for example:
public class SwatchPanel<TSw atch> : Panel
where TSwatch has static void Write(List<TSwa tch> foo)
Then this would be no problem.
As it is, we can't even write a generic Sum() function without
resorting to all kinds of ugly hacks, because the compiler can't see
the similarity between adding two ints and adding two floats. (We can
write a generic Max(), but only because the framework designers had the
foresight to make the basic types implement IComparable.)
Indeed. And that would be a bad way of writing it, IMO. It looks like you want polymorphism at that stage - either have a class which encapsulates a list of Swatches (generically) and override the behaviour appropriately, or have one Write method which uses polymorphism on the Swatch class to work out how to write each entry.
I agree that in this case, static methods are the wrong way to go. I'd
prefer the former solution, but it has the disadvantage that the list
class can't be located and instantiated automatically by the panel
without using reflection. You'd have to either construct the list
before the panel and pass it in to the constructor, or make the list
class an additional type parameter of SwatchPanel (with a new()
constraint).
Jesse
Jesse McGrew wrote: Jon wrote: steve bull <bu****@comcast .net> wrote: 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. That's not true at all. It doesn't allow the kind of templating C++ does, but frankly I'm glad of that.
I must say I agree with Steve. Generics seem to be useful for writing typesafe containers, avoiding casts, and not much else.
Well, I don't think that's quite true - but even if it were, isn't that
still a huge leap forward? Steve seems to be dismissing generics as
only useful in "trivial" situations, whereas I use collections the
whole time in real applications.
They'd be capable of so much more if only we had better constraints. If he could write, for example:
public class SwatchPanel<TSw atch> : Panel where TSwatch has static void Write(List<TSwa tch> foo)
Then this would be no problem.
Absolutely. And the language would be a lot more complicated, which is
something I don't like.
There are all kinds of things it would be nice to have contracts for -
it would be nice if an interface could specify the constructors or
static methds which must be available, for instance, so that plugins
loaded reflectively could provide methods of being instantiated
appropriately. However, once you start putting things
As it is, we can't even write a generic Sum() function without resorting to all kinds of ugly hacks, because the compiler can't see the similarity between adding two ints and adding two floats. (We can write a generic Max(), but only because the framework designers had the foresight to make the basic types implement IComparable.)
Yes, generics fails pretty badly in terms of operators. Indeed. And that would be a bad way of writing it, IMO. It looks like you want polymorphism at that stage - either have a class which encapsulates a list of Swatches (generically) and override the behaviour appropriately, or have one Write method which uses polymorphism on the Swatch class to work out how to write each entry.
I agree that in this case, static methods are the wrong way to go. I'd prefer the former solution, but it has the disadvantage that the list class can't be located and instantiated automatically by the panel without using reflection. You'd have to either construct the list before the panel and pass it in to the constructor, or make the list class an additional type parameter of SwatchPanel (with a new() constraint).
Yes. There are certainly issues there. I'd want to take a look at the
whole problem in a larger context, however. There may well be more
elegant solutions to what actually needs to be achieved if you move
further away from the existing attempted design.
Jon
"steve bull" <bu****@comcast .net> wrote in message
news:mo******** *************** *********@4ax.c om... 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<TSw atch> : System.Windows. Forms.Panel where TSwatch : Swatch { List<TSwatch> swatchList;
public MethodA { Swatch.Write(sw atchList); } }
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<Swat ch> colorSwatchList ) { write to swatch file }
public static void Write(List<Colo rSwatch> colorSwatchList ) { write to color swatch file }
public static void Write(List<Shad eSwatch> shadeSwatchList ) { write to shade swatch file } }
the compiler tells me
Can't convert from 'System.Collect ions.Generic.Li st<TSwatch>' to 'System.Collect ions.Generic.Li st<NewControls. Swatch>'
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<ColorSwatc h> 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
The issue with what you are trying to I believe is because determining which
overload of a method should be used is determined at COMPILE time. However,
information about the generic type will not be determined until runtime, so
the compiler has no way to use the other overloads. The only determination
that can be made at compile time is that TSwatch must be a Swatch, so the
first overload is used.
--
Adam Clauss
On Thu, 8 Dec 2005 07:13:21 -0000, Jon Skeet [C# MVP] <sk***@pobox.co m> wrote:
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 <snip>
Indeed. And that would be a bad way of writing it, IMO. It looks like you want polymorphism at that stage - either have a class which encapsulates a list of Swatches (generically) and override the behaviour appropriately, or have one Write method which uses polymorphism on the Swatch class to work out how to write each entry.
the problem with this suggestion is that it just pushes the problem down a level.
e.g. if I encapsulated the swatchList as SwatchList<TSwa tch> and call swatchList.Writ e() I would still need to get some
constant information associated with that swatch type in the new class. Which is the same problem in a different place. Alternativel y, have an instance method in SwatchPanel which writes its list out.
I am not quite sure what you mean here. Do you mean write routines
Write(List<Colo rSwatch) swatchList)
Write(List<Shad eSwatch> swatchList) etc?
If so it would seem to me that it would break all the reasons for using generics in the 1st place.
I still don't see any way forward with this - I seem to be better off going back to subclassing and using a non-typesafe
array. At least it avoids all these issues even though it is far from ideal.
Thanks for all the feedback.
Steve
steve bull <bu****@comcast .net> wrote: Indeed. And that would be a bad way of writing it, IMO. It looks like you want polymorphism at that stage - either have a class which encapsulates a list of Swatches (generically) and override the behaviour appropriately, or have one Write method which uses polymorphism on the Swatch class to work out how to write each entry. the problem with this suggestion is that it just pushes the problem down a level. e.g. if I encapsulated the swatchList as SwatchList<TSwa tch> and call swatchList.Writ e() I would still need to get some constant information associated with that swatch type in the new class. Which is the same problem in a different place.
No, it's not - because then you could ask for *instance* information,
and that could be overridden. If you have ColorSwatchList which extends
SwatchList<Colo rSwatch>, that could override appropriate methods to say
what headers to use etc. Alternativel y, have an instance method in SwatchPanel which writes its list out.
I am not quite sure what you mean here. Do you mean write routines
Write(List<Colo rSwatch) swatchList) Write(List<Shad eSwatch> swatchList) etc?
Just WriteList(), given that it would already know the list internally.
You wouldn't have different methods - you'd have one method which could
be overridden in classes which needed to.
If so it would seem to me that it would break all the reasons for using generics in the 1st place.
I still don't see any way forward with this - I seem to be better off going back to subclassing and using a non-typesafe array. At least it avoids all these issues even though it is far from ideal.
I would try looking at the problem from scratch, and try to avoid using
overloading as a poor man's form of polymorphism - that's not what it's
there for.
--
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
"steve bull" wrote: On Thu, 8 Dec 2005 07:13:21 -0000, Jon Skeet [C# MVP] <sk***@pobox.co m> wrote:
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 <snip>
Indeed. And that would be a bad way of writing it, IMO. It looks like you want polymorphism at that stage - either have a class which encapsulates a list of Swatches (generically) and override the behaviour appropriately, or have one Write method which uses polymorphism on the Swatch class to work out how to write each entry.
the problem with this suggestion is that it just pushes the problem down a level. e.g. if I encapsulated the swatchList as SwatchList<TSwa tch> and call swatchList.Writ e() I would still need to get some constant information associated with that swatch type in the new class. Which is the same problem in a different place.
no it doesn't. here's what Jon has suggested
method1, have different speicialized swatch list classes
ColorSwatchList : SwatchList<Colo rSwatch>
ShadeSwatchList : SwatchList<Shad eSwatch>
and each list will have a method Write() that knows how to write each entry
one by one.
method2, have each implementation of Swatch know how to write itself. then
the list will delegate the write operation to the individual entries.
both of which are the proper way of handle polymorphism. static methods are
not polymorphic. overloading them is not the right way to solve the problem.
Alternativel y, have an instance method in SwatchPanel which writes its list out.
I am not quite sure what you mean here. Do you mean write routines
Write(List<Colo rSwatch) swatchList) Write(List<Shad eSwatch> swatchList) etc?
If so it would seem to me that it would break all the reasons for using generics in the 1st place. I still don't see any way forward with this - I seem to be better off going back to subclassing and using a non-typesafe array. At least it avoids all these issues even though it is far from ideal.
Thanks for all the feedback.
Steve
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Justin Shen |
last post by:
you can define two class with same name but having different generic
parameters in one assembly. As below:
class Gen<T>
{
}
class Gen<T1,T2>
{
}
|
by: Tigger |
last post by:
I have an object which could be compared to a DataTable/List which I am
trying to genericify.
I've spent about a day so far in refactoring and in the process gone
through some hoops and hit some dead ends.
I'm posting this to get some feedback on wether I'm going in the right
direction, and at the same time hopefully save others from going
through the process.
|
by: Bilz |
last post by:
Ok, lets say I have a generic method:
public T Testing<T>() : where T:class
{
T myT = getStuff() as T;
// Do something with myT
return myT;
}
How do I call it dynamically? The following code does not work, but is
|
by: Bob Rock |
last post by:
Hello,
is there a way of retrieving the type of the class containing a static
method from the method itself when it gets called?
I have this situation: an abstact class that defines a static method, a
class that derives from the abstract class and I need
to retrieve the type of the concrete class when calling the static method.
public abstact class MyAbstact
|
by: gregory.lielens |
last post by:
Hello,
We are currently writing python bindings to an existing C++ library,
and we encountered
a problem that some of you may have solved (or that has found
unsolvable :( ):
A C++ class (let's call it CClass) is binded using classical Python
extension API to _PClass, which is accesible through python without
any
| |
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);
|
by: Steven D'Aprano |
last post by:
I have a class which is not intended to be instantiated. Instead of using
the class to creating an instance and then operate on it, I use the class
directly, with classmethods. Essentially, the class is used as a function
that keeps state from one call to the next.
The problem is that I don't know what to call such a thing! "Abstract
class" isn't right, because that implies that you should subclass the
class and then instantiate the...
|
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 methods can also create
forms but does not have reference to UI assembly.
I tried code below but got compile errows shown in comments.
How to fix ?
Andrus.
|
by: SimonDotException |
last post by:
I've written an abstract base type which uses generics to provide XML serialization and deserialization methods for use by its derived types, but I'm seemingly unable to write it in a way which completely satisfies FxCop code analysis. Here is the simplest form of the base class which reproduces the problem:
public abstract class XmlSerializableItem<T>
{
public string ToXml()
{
XmlSerializer xs = new XmlSerializer( this.GetType() );...
|
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...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |