in my code an error appears and says Overflow, i go into the de-bug thing and hover mouse over the variable "totalexp" and it says the value of it is "32767" but its not 32767.
here is the codeim using:
VB Code:
totalexp = totalexp + winexp
Printable View
in my code an error appears and says Overflow, i go into the de-bug thing and hover mouse over the variable "totalexp" and it says the value of it is "32767" but its not 32767.
here is the codeim using:
VB Code:
totalexp = totalexp + winexp
so before that set the code to read:
VB Code:
totalexp = 0
its not always going to be "0"
Can you post some more code for us to see. May be variable is getting some value greater than its extreme limit, therefore , overflow is occuring.
ok:
(thats the whole function that that snippet of code was from.VB Code:
Private Function battlewon(done As Boolean) If totalene <= 0 Then gold = gold + wingold totalexp = totalexp + winexp If exp - winexp > 0 Then lbltext.Caption = "You won the battle. Total EXP: " & winexp & "" GoTo doneturn: Else Do While exp - winexp <= 0 winexp = winexp - exp addon = needexp / 100 addon2 = addon * 11 needexp = needexp + addon2 exp = needexp exp = exp - winexp level = level + 1 finish (True) If level >= 4 Then If level <= 10 Then maxhp = maxhp + 3 maxmp = maxmp + 1 fp = fp + 1 dp = dp + 1 GoTo addedstats: End If If level <= 13 Then maxhp = maxhp + 5 maxmp = maxmp + 1 fp = fp + 1 dp = dp + 1 GoTo addedstats: End If If level <= 18 Then maxhp = maxhp + 10 maxmp = maxmp + 2 If playerclass = "Wizard" Then maxmp = maxmp + 1 End If If playerclass = "Warrior" Then fp = fp + 1 End If If playerclass = "Soldier" Then dp = dp + 1 End If fp = fp + 1 dp = dp + 1 GoTo addedstats: End If If level <= 22 Then maxhp = maxhp + 18 maxmp = maxmp + 2 If playerclass = "Wizard" Then maxmp = maxmp + 1 End If If playerclass = "Warrior" Then fp = fp + 1 End If If playerclass = "Soldier" Then dp = dp + 1 End If fp = fp + 2 dp = dp + 1 GoTo addedstats: End If If level <= 28 Then maxhp = maxhp + 25 maxmp = maxmp + 2 If playerclass = "Wizard" Then maxmp = maxmp + 1 End If fp = fp + 2 dp = dp + 2 GoTo addedstats: End If If level <= 35 Then maxhp = maxhp + 35 maxmp = maxmp + 3 fp = fp + 2 dp = dp + 2 GoTo addedstats: End If If level <= 42 Then maxhp = maxhp + 50 maxmp = maxmp + 3 fp = fp + 3 dp = dp + 3 GoTo addedstats: End If If level <= 50 Then maxhp = maxhp + 70 maxmp = maxmp + 4 If playerclass = "Wizard" Then maxmp = maxmp + 1 End If If playerclass = "Warrior" Then fp = fp + 1 End If If playerclass = "Soldier" Then dp = dp + 1 End If fp = fp + 3 dp = dp + 3 GoTo addedstats: End If If level <= 60 Then maxhp = maxhp + 85 maxmp = maxmp + 6 If playerclass = "Wizard" Then maxmp = maxmp + 1 End If fp = fp + 3 dp = dp + 3 GoTo addedstats: End If If level <= 75 Then maxhp = maxhp + 100 maxmp = maxmp + 8 If playerclass = "Wizard" Then maxmp = maxmp + 1 End If If playerclass = "Warrior" Then fp = fp + 1 End If If playerclass = "Soldier" Then dp = dp + 1 End If fp = fp + 4 dp = dp + 3 GoTo addedstats: End If If level <= 88 Then maxhp = maxhp + 120 maxmp = maxmp + 9 If playerclass = "Wizard" Then maxmp = maxmp + 1 End If fp = fp + 5 dp = dp + 5 GoTo addedstats: End If If level <= 98 Then maxhp = maxhp + 200 maxmp = maxmp + 10 fp = fp + 7 dp = dp + 7 GoTo addedstats: End If If level >= 99 Then GoTo addedstats: End If End If maxhp = maxhp + 2 addedstats: If level > 99 Then level = 99 End If If maxhp > 9999 Then maxhp = 9999 End If If curhp > maxhp Then curhp = maxhp End If If maxmp > 999 Then maxmp = 999 End If If curmp > maxmp Then curmp = maxmp End If GoTo doneturn: Loop End If doneturn: done = False End If End Function
Variables are set to zero by default, so a totalexp = 0 is unnecessary, and would reset the variable to zero on every pass. I doubt he wants that.
Try doing a Debug.Print on winexp and see what you get.
whats a Debug.Print?
That tells the debugger to display the value of winexp. Basically it's the same thing as hovering over the variable itself. This is what I do because I don't always get the results of a variable just by hovering over it.
Oh, there's another point I wanted to make:
I notice that there's no variable declaration in your function; I'm assuming you've got this in the General Declarations section of your form?
Also, it's good practice to put an Option Explicit in the first line of the General Declarations section. This forces all variables to be declared before they are used, and will save you a LOT of headaches in debugging and error tracking.
After doing your Option Explicit, run your program. If it errors out with "Undeclared Variable" or something like that, put the following:
VB Code:
Dim totalexp As Long Dim winexp As Long
Note the use of Long variables. It's good to use long variables in the kind of program you're developing. I am thinking that's why you're getting the Overflow error. Integers can only handle between the numbers -355 and 355, if I remember correctly. I have successfully worked with numbers way up in the millions with Long variables, so you really shouldn't get an overflow unless totalexp goes beyond that range.
Nope. Integer limits go from -32768 to 32767.Quote:
Originally posted by hothead
Integers can only handle between the numbers -355 and 355, if I remember correctly.
:)