argument out of range Exception was unhandled

Asked

Viewed 234 times

0

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

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

I was wondering if you have any code to specify that the maximum you can get is 100

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

1 answer

1


Use Math.Min

I was wondering if you have any code to specify that as much as you can reach is 100 and do not go beyond that

To min function class Math can do this maximum value control. It will compare the two values and return the smallest of them. If Fome pass 100, will return 100.

Example:

ProgressBar2.Value = Math.Min(Fome, 100)
  • Thanks for the help, it worked.

  • @Lukasoliver If it worked, don’t forget to mark the answer as right. =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.