Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello! I'm trying to make a simple game where I shoot an object, in C#.
When I try to make their posisition random, this is the error i Get:

System.ArgumentOutOfRangeException: ''minValue' cannot be greater than 'maxvalue'



Here's the piece of code where the problem is:

private void timerGameLoop_Tick(object sender, EventArgs e)
        {
            UpdateVar();
            this.Refresh();
        }
        private void UpdateVar()
            
        {
            
            cVarandas.Update(
           rnd.Next(Resources.vhappy.Width, this.Width - Resources.vhappy.Width), 
           rnd.Next(this.Height/2 , this.Height - Resources.vhappy.Height * 2)
                  );
        }

Please help!

What I have tried:

I honestly don't know what to do here
Posted
Updated 28-Mar-20 7:21am

1 solution

Read the error message, it couldn't be any clearer:
System.ArgumentOutOfRangeException: ''minValue' cannot be greater than 'maxvalue'
The version of Random.Next you are calling takes two parameters: Random.Next Method (System) | Microsoft Docs[^] the first is the minimum value and the second is the maximum value.
The error is saying that the minimum value is greater than the maximum value which means it can't return any values at all!

Swap 'em over, so the smallest is first ... or use the debugger to find out why they are like that.
 
Share this answer
 
Comments
Lousada SCP 28-Mar-20 14:05pm    
Thank you so much, it worked. But now I have another question, would you mind to help me again? I posted it here too

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