473,769 Members | 5,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it possible to call a static method in the type class of a generic class?


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
}
}
Dec 8 '05
10 2320


Thank you for taking up so much of your time to make all these suggestions.

I don't think generics are the natural way of dealing with my problem. The way I originally developed the SwatchPanel as
a series of subclasses worked pretty well where SwatchPanel did most of the work. Thinking about what you have talked
about leads me to believe that the original reason for trying generics might be overcome with some of your suggestions.

Using subclasses only had the problem of passing around a List<Swatch> to the read/write routines which I wanted to be
cast to the swatch type appropriate to those routines. I think that encapsulating the swatchList as you described would
work there as well. What I have also works, it doesn't break any OO rules I don't think and I don't need to retest
everything again.

In fact, using generics, read/write wasn't the only problem I had - just the biggest of them. For example when I draw
the swatches on the panel they could be different sizes depending on the type of swatch and this would be a SwatchPanel
parameter really as I could decide, and have, to write other widgets which have different sizes that they would have the
swatches draw themselves.

Your explanations have been much appreciated.

Thanks,
Steve


On Thu, 8 Dec 2005 18:05:29 -0000, Jon Skeet [C# MVP] <sk***@pobox.co m> wrote:
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<Col orSwatch>, 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.


Dec 8 '05 #11

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

Similar topics

9
1243
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> { }
3
2772
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.
2
1752
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
8
1243
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
1
2426
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
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);
44
2954
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...
2
2472
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.
0
2988
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() );...
0
9589
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
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
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, 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...
0
10049
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
9997
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
8873
agi2029
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...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3965
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
2815
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.