weird asp behavior

Results 1 to 2 of 2

Thread: weird asp behavior

  1. #1
    Join Date
    Dec 1969

    Default weird asp behavior

    I was designing a site today and found something rather odd. I have defined VBScript classes to use on my page, one class holds 2 arrays, one for dates and one for hours worked on a particular date. i use a function called getHours that takes a date as a parameter and then loops through the array of dates until it finds a matching date, that index then corresponds to the array for the hours. if the date isn&#039;t found it returns 0 other wise it returns the hours. in my loop i use i for my control variable, but when i go to use this function on a page inside a loop that also uses i, the page won&#039;t errors , my page file just starts climbing on my computer. once i changed the control variable in one of the loops it ran fine. i just find this quite troublesome and can&#039;t really figure out why this happens, or if i should be doing something different. any thoughts or suggestions are appreciated.<BR>thanks

  2. #2
    Join Date
    Dec 1969

    Default Not weird at all...expected!

    You need to learn about *SCOPE*.<BR><BR>In VBS, as in most languages, if a variable is declared at global scope and is *not* declared at subroutine scope, then even in the subroutine the global variable is used.<BR><BR>In VBS, if you do NOT DECLARE a variable (that is, using DIM) *at all*, then it is automatically a global variable.<BR><BR>SO... In your code, that meant that *ALL* uses of your variable "i" were using *the same variable*!!!! No surprise it kablooeys!<BR><BR>The solution is easy. Put<BR>&nbsp; &nbsp; &nbsp; DIM i<BR>*inside* EACH sub/function where you use it as a local variable.<BR><BR>In general, you should DIM *all* variables used in a Sub/Function *unless* you absolutely *know* that you intend to use a global variable. And in any case you should DIM *ALL* variables at some point, either globally or locally.<BR><BR>This is pretty fundamental to the use of subroutines, so write it on your mental blackboard many times: "I *will* declare my variables in the proper scope. &nbsp; I *will* declare my ...."<BR><BR><BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts