Click here to Skip to main content
16,020,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear all,

It is a big problem corresponds my application. My application is abnormally terminated showing an exception "Out of range value of Int32".

But I have tested by the demo code with a button click on a winform:
int j;
j = Int32.MaxValue;
Random rnd = new Random(10000);
j = j * rnd.Next();
MessageBox.Show("Converted "+j);




But the application didn't terminate.

What might be th rreason???:confused:

Please help me.
Posted

the reason it isn't working is that you already assign the highest value an Int32 can hold:

j = Int32.MaxValue;


and then you try to set it's value as itself multiplied by your randomly generated number - the result is too big for an Int32 to store.

my guess is you need a long instead of an int. have a look at this article and see if it helps:

[http://www.blackwasp.co.uk/CSharpNumericDataTypes.aspx]
 
Share this answer
 
Comments
fjdiewornncalwe 4-Nov-10 8:30am    
I think that was his point. The code he has provided us is code that works, not the code that fails.
Example:
long j;
j = Int32.MaxValue;
Random rnd = new Random(10000);
j = j * rnd.Next();
MessageBox.Show("Converted " + j);

because if you multiple 2 Int32 value then it may be Int64 value, which is long ;)

Please vote and Accept Answer if it Helped.
 
Share this answer
 
Comments
Sauro Viti 4-Nov-10 13:05pm    
As he assigned j = Int32.MaxValue the only chance to not overflow on the product is to get 1 as random number...
I don't think the error you are getting is from the Int32 overflow. In fact the message you posted "Out of range value of Int32" does not even seem to be a .NET exception message. Perhaps you are trying to insert this value into a database? And maybe it's the DB that's throwing an exception with this message.
 
Share this answer
 
You may need to do some digging... If you try this test code block as a windows console app you'll see that what is happening in your test code is that the value is wrapping. i.e. When it is invalid, it wraps as a negative value. That means you are doing something different in the code that is failing as opposed to the code that works as this does.

C#
class Program
    {
        static void Main(string[] args)
        {
            int j = Int32.MaxValue;
            int r = 0;
            int i = 0;
            Random rnd = new Random(10000);
            try
            {
                r = rnd.Next();
                for (i = 0; i < 10; i++)
                {
                    Console.Write( 
                        String.Format( "i={0}, j={1}, r={2}:   
                            result=", i, j, r ));
                    j *= r;
                    Console.WriteLine( j );
                }
            }
            catch( Exception err )
            {
                Console.WriteLine( 
                    String.Format( "Error( i={0}, j={1}, r={2} ): 
                        {3}", i, j, r, err.Message ));
            }
            Console.WriteLine( string.Empty );
            Console.WriteLine( "Press Any Key To Continue");
            Console.ReadKey();
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900