Beware constants in ASP (scripting bug)

Published by marco on

Updated by marco on

There is a bug in scope resolution in IIS 5.0. When resolving a variable within a member function, precedence is given to a global constant instead of to a member variable of the same name. The problem does not occur with global variables. Paste the following code into a page:

Sample Code

<%
const name = 1
 
class A
 
  public name
 
  public function get_name
    get_name = name
  end function
 
end class
 
dim a1
set a1 = new A
 
a1.name = "test"
 
%>
<%=name%><br>
<%=a1.name%><br>
<%=a1.get_name%><br>

All it does is declare a constant, a class with one function, then outputs some data. The expected output is:

1
test
test

However, IIS emits:

1
test
1

If you replace:

const name = 1

with:

dim name
name = 1

it emits:

1
test
test

as expected.

If you replace:

public function get_name
  get_name = name
end function

with (change is highlighted):

public function get_name
  get_name = me.name
end function

it emits:

1
test
test

as expected.

<sarcasm>Here’s looking forward to .NET.</sarcasm>