tags:

views:

157

answers:

3

Am I being blind, or does the .NET framework not provide any kind of ranged integer class? That is, a type that would prevent you setting a value outside some given bounds that are not the full range of the basic data type. For example, an integer type that would restrict its values to between 1 and 100. Showing my age here, but back in '93, I remember using that sort of thing in Modula-2 (eeek!), but I've not seen explicit framework / language support for it since.

Am I just missing something, or is it a case of "it's so simple to make your own that the framework doesn't bother"?

Cheers.

+1  A: 

There isn't a built-in class to support this, but typically, it's very easy to enforce in a property setter that it's normally not considered necessary.

Without properties, this would be much more useful. However, since you can do:

private int myRangedIntValue;
public int MyRangedIntValue
{
    get { return myRangedIntValue; }
    set 
    { 
        myRangedIntValue = Math.Max(1, Math.Min(100, value));
    }
}

The advantages of a custom type diminish. By leaving the standard int types, you have that many fewer types to worry about for compilation, data binding, etc.

Reed Copsey
This is useful code, but keep in mind that there is an implicit assumption with this code that we should coerce any value > 100 or < 1 to a value between 1-100. In certain situations you might not want to coerce the value but rather throw an exception or fail in another way if the value was outside of the bounds.
CrimsonX
Yes - this was just an example showing how you can accomplish this with properties - that being said, properties are MUCH more flexible, since you, as the author, have control over whether to clamp your value, ignore invalid values, raise an exception, etc. A custom type would make this difficult...
Reed Copsey
+1  A: 

You’re not missing anything – unless enums fit your case1) – it doesn’t exist. Roll your own.


1) And notice that enums actually don’t enforce a valid enum value because that would preclude of using enums for combined flags.

Konrad Rudolph
Yep, enums are what triggered the question. I recently saw some code that relied on enums to prevent invalid values, but I thought it wasn't reliable for the reason you mention.
Mal Ross
default(someEnumType) might actually returns an invalid value (it always returns zero for a enum)
Rune FS
+1  A: 

They're not baked in like Eiffel's design by contract, but you can roll your own. For C# 3.5 and earlier, there's Spec#.

For C# 4.0, they've introduced CodeContracts (but it seems to be in BCL extensions).

48klocs
Thanks for the links; will check them out. :)
Mal Ross
Note that Spec# is just an experiment. They actually re-implemented the C# compiler to accomplish this, and it's not in sync with the official compiler, so if you want the real deal, you'll have to wait for code contracts in .NET 4.0. For .NET 3.5 I suggest you look here: http://research.microsoft.com/en-us/projects/contracts/ (Note: Academic license only. The real deal is still in .NET 4.0.)
Robert Harvey