|
|||
|
| |||
Loading
|
|||
|
| |||
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Oct 25, 2010, 5:07 AM
JurePosted Oct 24, 2010, 1:31 PM
int range_size = 256;
int range_start = 1;
int temp = /* your calculation */ - range_start
numColors = temp % range_size + range_start
and you can, for example, pack all this into a property:
private int num_colors;
private int range_size = 256;
private int range_start = 1;
public int NumColors { get { return num_colors; } set { num_colors = (value - range_start) % range_size + range_start; } }
so now, you can simply write:
NumColors *= 1000;
and it would set NumColors (num_colors indirectly) to your selected range (1-256).
But, modulo doesn't work as you want it to with negative numbers in your case. -1000 % 256 will return -1000, so you need to do 257 - |-1000| % 256, which in code is:
public int NumColors {
get { return num_colors; }
set
{
value -= range_start;
if (value < 0)
num_colors = range_size - (-value % range_size) + range_start;
else
num_colors = value % range_size + range_start;
}
}
and now you can do this:
NumColor -= 1000;
JurePosted Oct 25, 2010, 1:40 PM
I've also completely rewritten the code that's posted here, it's simpler and works without modulus operation.
Mike SmithPosted Oct 25, 2010, 11:53 AM
I was really exploring C# rather than trying to 'translate' a specific concept for a particular program. I can't remember offhand how Delphi would respond to an infringement and I don't seem to be able to get my legacy compiler up and running to try it out. However, I feel, as Jure and you point out, that C# makes explicit (in this case at least) what Delphi keeps as implicit and hidden behind the scenes.
I'm all for the 'explicit' approach and perhaps I shouldn't try to be too clever!
Thanks again to you both...
Mike SmithPosted Oct 25, 2010, 4:36 AM
Mike
Sam HobbsPosted Oct 24, 2010, 3:56 PM
Actually I think that Jure's samples do more than what the Delphi sample does. Delphi does appear to have a more productive range checking feature but I assume C# is as productive or more productive in other ways.