473,386 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Mandatory static methods

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"?
Nov 15 '05 #1
6 1849
Ranier Dunno <eb*****@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
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).
Nov 15 '05 #3
Ranier Dunno <eb*****@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #4
>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, theunits that the class uses could be implemented as a type- levelattribute.


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.
Nov 15 '05 #5
Ranier Dunno <eb*****@hotmail.com> wrote:
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...)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #6
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 namesabove, 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...).
Nov 15 '05 #7

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

Similar topics

13
by: Axehelm | last post by:
Okay, I'm in a debate over whether or not static methods are a good idea in a general domain class. I'm personally not a fan of static methods but we seem to be using them to load an object. ...
3
by: Steven D'Aprano | last post by:
I've been doing a lot of reading about static methods in Python, and I'm not exactly sure what they are useful for or why they were introduced. Here is a typical description of them, this one...
8
by: Steven Livingstone | last post by:
Anyone able to explain to me why you cannot define an interface that can then be implemented using static methods? I understand the C# CLS states this, but just interested in the reasons behind...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
5
by: blah, blah, blah | last post by:
I'm developing a .Net web application and created many helper classes often using static (shared in VB.Net) methods. Do I need to use the lock (SyncLock) statement in these methods to prevent...
4
by: Joe Fallon | last post by:
In another post Kevin Spencer stated: "one should be careful of using static fields, properties, and methods, by understanding what the implications of such are (e.g. locking static variables when...
15
by: dn | last post by:
I'm starting an n-tier application with an ASP.NET 2.0 presentation layer, a business layer, a data access layer, and a SQL Server 2005 database, and I have a question. In the business and data...
6
by: MSDNAndi | last post by:
Hi, I have a baseclass (non-static) with some static and some non-static methods/fields/properties. In the baseclass in one of the static methods I need to do something like " somelogic...
12
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.