|
|||
|
| |||
Loading
|
|||
|
| |||
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
JureRank #2,128 · 670 reputationPosted Oct 24, 2010, 9:31 AM EDT
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;
Sam HobbsRank #107 · 20.5K reputationPosted Oct 25, 2010, 1:07 AM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Oct 24, 2010, 11:56 AM EDT
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.
Mike Smith6 reputationPosted Oct 25, 2010, 12:36 AM EDT
Mike
Mike Smith6 reputationPosted Oct 25, 2010, 7:53 AM EDT
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...
JureRank #2,128 · 670 reputationPosted Oct 25, 2010, 9:40 AM EDT
I've also completely rewritten the code that's posted here, it's simpler and works without modulus operation.