33

I can't understand this error:

In this call to method SetVolume, Volume = 2055786000 and size = 93552000. Volume is an Integer property, and size is also Integer, as you can see.

The class is a partial class of a dbml entity class, however this Volume property is NOT a column in the database, it exist only in the partial class, as a "business object property".

View Detail shows:

Data> Item : In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.

alt text

What may cause this...?

asked Jan 21, 2011 at 7:50

8 Answers 8

44

The maximum value of an integer (which is signed) is 2147483647. If that value overflows, an exception is thrown to prevent unexpected behavior of your program.

If that exception wouldn't be thrown, you'd have a value of -2145629296 for your Volume, which is most probably not wanted.

Solution: Use an Int64 for your volume. With a max value of 9223372036854775807, you're probably more on the safe side.

answered Jan 21, 2011 at 7:54
Sign up to request clarification or add additional context in comments.

3 Comments

I was creating sum of first 10,000 natural numbers when I encountered this error. I was using Int32 data type for the summation variable. When I changed it to Int64 then the issue got resolved. Thanks. It helped!
Is there a way to let it overflow without casting an exception? In my case an overflow is fine.
@VapidLinus The unchecked keyword lets you do that.
8
int.MaxValue = 2147483647
2055786000 +たす 93552000 = 2149338000 > int.MaxValue

So you cannot store this number into an integer. You could use Int64 type which has a maximum value of 9,223,372,036,854,775,807.

answered Jan 21, 2011 at 7:54

Comments

3

For simplicity I will use bytes:

byte a=250;
byte b=8;
byte c=a+b;

if a, b, and c were 'int', you would expect 258, but in the case of 'byte', the expected result would be 2 (258 & 0xFF), but in a Windows application you get an exception, in a console one you may not (I don't, but this may depend on IDE, I use SharpDevelop).

Sometimes, however, that behaviour is desired (e.g. you only care about the lower 8 bits of the result).

You could do the following:

byte a=250;
byte b=8;
byte c=(byte)((int)a + (int)b);

This way both 'a' and 'b' are converted to 'int', added, then casted back to 'byte'.

To be on the safe side, you may also want to try:

...
byte c=(byte)(((int)a + (int)b) & 0xFF);

Or if you really want that behaviour, the much simpler way of doing the above is:

unchecked
{
 byte a=250;
 byte b=8;
 byte c=a+b;
}

Or declare your variables first, then do the math in the 'unchecked' section.

Alternately, if you want to force the checking of overflow, use 'checked' instead.

Hope this clears things up.

Nurchi

P.S.

Trust me, that exception is your friend :)

answered Aug 22, 2013 at 23:16

Comments

2

The result integer value is out of the range which an integer data type can hold.

Try using Int64

answered Jan 21, 2011 at 7:54

Comments

2

This error occurred for me when a value was returned as -1.#IND due to a division by zero. More info on IEEE floating-point exceptions in C++ here on SO and by John Cook

For the one who has downvoted this answer (and did not specify why), the reason why this answer can be significant to some is that a division by zero will lead to an infinitely large number and thus a value that doesn't fit in an Int32 (or even Int64). So the error you receive will be the same (Arithmetic operation resulted in an overflow) but the reason is slightly different.

answered Apr 7, 2014 at 10:04

2 Comments

I actually upvoted as this is good information, but I assume the downvote is becuase the title says "Adding Integers", and you're not adding integers.
thanks, and of course you are right, hadn't noticed that part
1

Maximum value fo int is 2147483647, so 2055786000+93552000> 2147483647 and it caused overflow

answered Jan 21, 2011 at 7:54

Comments

0

2055786000 +たす 93552000 = 2149338000, which is greater than 2^31. So if you're using signed integers coded on 4 bytes, the result of the operation doesn't fit and you get an overflow exception.

answered Jan 21, 2011 at 7:56

Comments

0

The maximum size for an int is 2147483647. You could use an Int64/Long which is far larger.

answered Jan 21, 2011 at 7:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.