argument out of range exception was unhandled

0

I'm a beginner in VB.NET and I'm creating a simple simulation game, have hunger, thirst etc, in progressbar hunger the maximum that arrives is 100.

When you eat something restores 5, if I'm 98 and how it gives error.

I would like to know if you have any code to specify that the maximum that can reach is 100 and not to pass it

Like for example being 98 hungry and instead of going to 103 go to 100, only restoring 2.

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    ProgressBar2.Value = Fome
Module Module1
Public Fome As Integer = 100
    
asked by anonymous 09.02.2017 / 20:37

1 answer

1

Use Math.Min

  

I wanted to know if you have any code to specify that the maximum you can   reach is 100 and do not pass it

The Min function of class Math can do this maximum value control. It will compare the two values and return the smaller of them. If Fome exceeds 100, it returns 100.

Example:

ProgressBar2.Value = Math.Min(Fome, 100)
    
09.02.2017 / 21:02