The other alternative I can see would be using an int and clamping the value somehow
Using int
is exactly what I would do.
but exposing it as an int might give the user the wrong intent of the code.
So make the intent so explicit that the user cannot possibly get the wrong idea:
public class Spritesheet
{
public Spritesheet(int spriteCount)
{
if (spriteCount < 0 || spriteCount > 256)
throw new ArgumentOutOfRangeException ("spriteCount", "Number of sprites must be between 0 and 256 (inclusive)");
// ...
}
}
Even if the user at any point gets the idea that spriteCount
can be 1000, that idea will fade very quickly after trying it.
I renamed spriteToIndex
in response to Richard Schneider's comments, who quite understandably interpreted the meaning of the variable name differently. The word "Index" in it could be read as suggesting that the value must be an index, rather than the total number of sprites.