Groups | Blog | Home
all groups > c# > august 2003 >

c# : Mandatory static methods



Ranier Dunno
8/10/2003 11:56:13 PM
Hi,

I am wondering if there is some construct to force a
class to implement a set of static methods? Sort of like
a "class interface" as opposed to the common "object
Ranier Dunno
8/11/2003 12:22:03 AM
Ok, that's what I thought, thanks. The reason I was
asking, is actually that I want to add some "reflection-
like" properties to a set of objects I have. My objects
have properties which have values in a given scientific
unit, and I want to provide the ability to query a class
about its properties and the scientific units of those
properties. Since all objects of the same class will have
identical properties and units, it made sense to make the
Ranier Dunno
8/11/2003 12:41:39 AM
[quoted text, click to view]

OK, brilliant! I'll look into that - I am definitely
looking for a more elegant way of doing things. In
essence, I want to do this: say, I have a "Sprint" class
representing short runs made by a single person on a
track field. A "Sprint" will of course have some non-
static properties, like the length of the sprint, name of
the runner, time spent, average velocity and so forth.
What I would like to do, is to add some meta-data for
those properties. For instance, I would like the ability
to specify that length is measured in meters, not in
Ranier Dunno
8/11/2003 1:12:30 AM

[quoted text, click to view]
but...)

I'll go for that, it's just what the doctor ordered! :-)
Thanks a lot, I was completely unaware of that C# feature
Jon Skeet
8/11/2003 8:11:20 AM
[quoted text, click to view]

No. It's a common question, but there's no way of forcing either
constructors or static methods to be implemented. The reason is that
these methods/constructors would have to be called by reflection
anyway, but it would be handy to be able to get the compiler to check
that a well known method, or a specific constructor was implemented.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet/
Jon Skeet
8/11/2003 8:26:12 AM
[quoted text, click to view]

I'm not entirely clear on what you're doing here, but you may want to
consider using custom attributes for some of this - for instance, the
units that the class uses could be implemented as a type-level
attribute.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet/
Jon Skeet
8/11/2003 8:52:40 AM
[quoted text, click to view]

Right. I would have attributes such as:

[LengthUnit(LengthUnits.Metre)]
[TimeUnit(TimeUnits.Second)]

either at a type or method level. (I'm not comfortable with the names
above, as LengthUnits suggests it's a flag-based enum, but...)

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet/
AddThis Social Bookmark Button