Why int remainder=(int)(Math.DivRem(MaxValue, 2, out remainder)) is 5 where int MaxValue = 10!!!;

  • Thread starter Thread starter Alexander Gorbylev
  • Start date Start date
A

Alexander Gorbylev

Hi all!

Test:

-----------------------------------

int MaxValue = 10; // =current value

int remainder=(int)(Math.DivRem(MaxValue, 2, out remainder));

RectangleF[] fillRectangles = new RectangleF[(int)(MaxValue / 2) +
remainder];

---------------------------------

(int)(MaxValue / 2) = 5 - it's right,

BUT:

remainder is 5

!!!!!!!

Why? Total fillRectangles.Lenght = 10

:-)))
 
int MaxValue = 10; // =current value

int remainder=(int)(Math.DivRem(MaxValue, 2, out remainder));

You're assigning a value to remainder twice in this statement - once
with the return value of DivRem, and once with the out parameter. The
out parameter will be assigned first, then the return value. The
return value is 5, hence "remainder" is 5 after the statement has
executed.

If you *only* want the remainder, just use the % operator or don't use
the result of DivRem. If you want both the remainder and the divisor,
use two different variables.

Jon
 
int remainder=(int)(Math.DivRem(MaxValue, 2, out remainder));


the out parameter is overwritten by the return value.


Perhaps want you want to do is:


int remainder;
int result = (int)(Math.DivRem(MaxValue, 2, out remainder));


ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top