Mandatory static methods

  • Thread starter Thread starter Ranier Dunno
  • Start date Start date
R

Ranier Dunno

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
interface"?
 
Ranier Dunno said:
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
interface"?

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.
 
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
methods static (plus I don't want larger objects).
 
Ranier Dunno said:
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
methods static (plus I don't want larger objects).

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.
 
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.

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
feet, and that velocity is meters/second, not mph.
 
Ranier Dunno said:
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
feet, and that velocity is meters/second, not mph.

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...)
 
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...)

I'll go for that, it's just what the doctor ordered! :-)
Thanks a lot, I was completely unaware of that C# feature
(I'm a converted Java programmer...).
 
Back
Top