How to "undim" a variable

GrrWolfie
GrrWolfie used Ask the Experts™
on
I'm building a dynamic website with ASP file Includes, i'm running into "Name redefined" errors as sometimes two files will Dim the same variable (and I want to use Option Explicit). I REALLY don't want to give each file unique variable names as it'll be hell to copy paste code pieces around.

Example:
file1.asp:
<%
Dim somedata
somedata="hello!"
Set somedata=Nothing
%>
file2.asp:
<%
Dim somedata
somedata="goodbye!"
Set somedata=Nothing
%>
index.asp:
<!--#INCLUDE FILE="file1.asp" -->
<!--#INCLUDE FILE="file2.asp" -->
Output:
Microsoft VBScript compilation (0x800A0411)
Name redefined

Also I understand Subs and Functions will delete internally declared variables after losing scope, i'm looking for a more robust solution.

Thanks in advance!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
There is no way to "undim" a var. The only way to achieve vars to be local somehow is to put them in an own scope. Putting DIMs into ASP.NET code makes them appear at the module level, available to all code on that page. You can
a) create functions, and call them immediately
b) use "pseudo" commands to build an own scope.
The latter would look like this
if true then
  Dim a
  ' ...
end if
...
if true then
  Dim a
  ...
end if

Open in new window

Regarding scopes see http://msdn.microsoft.com/en-us/library/1t0wsc67.aspx .

Author

Commented:
Qlemo... very cryptic example... lol

Temporary Solution that meets my question:
file1.asp
If isNULL(somedata) Then
End If
If Err.number<>0 then
      definethis="Dim somedata"
      Execute(definethis)
End If
somedata="hello!"
file2.asp
If isNULL(somedata) Then
End If
If Err.number<>0 then
      definethis="Dim somedata"
      Execute(definethis)
End If
somedata="goodbye!"
index.asp:
<%
Option Explicit
On Error Resume Next
%>
<!--#INCLUDE FILE="file1.asp" -->
<!--#INCLUDE FILE="file2.asp" -->
Response.Write "Success!"
Output:
Success!

Working on a better solution.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
That works if you want to have the var defined, and use it on different locations for the same meaning. But in that case it is better to separate into a single declaration and several definition (= code) parts. What you do is not consistent - different code parts should not use the same var for different values, if the code parts are independent from each other.
Unfortunately you cannot unDim variable in VB Script.

Solution for that would be pre-processing with batch file that renames the variable:
"replace" file2.asp file2a.asp var1 var1a

that will generate file file2a.asp where the variable var1 has been replaces with the variable var1a

then you will use
<!--#INCLUDE FILE="file1.asp" -->
<!--#INCLUDE FILE="file2a.asp" -->
without any issues.
---------------------------------------
There are many tools to replace text in the file according to the pattern.
Unix has batch editor SED and there is SED for Windows.

It will look like

SED file2.asp file2a.asp pattern1 pattern1a

Or it could be a file with patterns like:
var1 var1a
var2 var2a
var3 var3a
...

You can use whatever is convenient for you that do the job.
---------------------------------------
Finally you will have:
1. ...ASP pages have been changed
2. Run ...bat that will replace all conflict variables (or schedule it on a regular basis)

If you get issues with changes - additional conflict variables appeared - adjust the list of patterns.
---------------------------------------
Even if you will not find a convenient editor - The program ~ 50 lines of source code could be implemented with VB, C#, C++, etc to do that.
Top Expert 2007
Commented:
>>I REALLY don't want to give each file unique variable names as it'll be hell to copy paste code pieces around.

Please explain why different code snippets will use the same variables for different things?

You could create classes in your code; different objects can have the same properties and/or methods without conflicting with one another.

Author

Commented:
Qlemo - I should have stated what Badotz asked for first so the problem would have been more understandable, sorry buddy.

fomand - Thanks for using my example always makes it easier :). Yes the batch file would work however that's a hell of a lot of server overhead lol. I would more likely use a ton of Execute() commands.

Badotz - I have a file that pulls data from text files into variables. In some instances I need to pull data from several files one at a time so the "loader" file needs to be included more than once. I make changes to this file on and ongoing basis so I don't want to have to have copies of it with different variable names.

Latest fix:

var_define.asp:
<% Dim somedata %>
var_clean.asp:
<% Set somedata=Nothing %>
file1.asp:
<% somedata="hello!" %>
file2.asp:
<% somedata="goodbye!" %>
index.asp:
<!--#INCLUDE FILE="var_define.asp" -->
<!--#INCLUDE FILE="file1.asp" -->
Response.Write somedata&"<br>"
<!--#INCLUDE FILE="var_clean.asp" -->
<!--#INCLUDE FILE="file2.asp" -->
Response.Write somedata&"<br>"
<!--#INCLUDE FILE="vars_clean.asp" -->
Output:
hello!
goodbye!

Here's the best solution so far. It provides the declaration and clean-up I need between files. Working on a better one but this seems pretty good.
In better keeping with the description of what i'm trying to do it should actually be this and I made it a little better...

hello.inc:
hello!
goodbye.inc:
goodbye!
define.asp:
<%
Dim filetopullfrom
Dim somedata
Function clean()
  Set filetopullfrom=Nothing
  Set somedata=Nothing
End Function
%>
gettxtdata.asp:
<% somedata="data from filetopullfrom file" %>
index.asp:
<% @LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<% Option Explicit%>
<!--#INCLUDE FILE="define.asp" --><%
filetopullfrom="hello.asp"
%><!--#INCLUDE FILE="gettxtdata.asp" --><%
Response.Write somedata&"<br>"
clean()
filetopullfrom="goodbye.asp"
%><!--#INCLUDE FILE="gettxtdata.asp" --><%
Response.Write somedata&"<br>"
clean()
%>
Output:
hello!
goodbye!
Top Expert 2007

Commented:
I just don't grok why you need to do this?

Your examples do not show your intentions very clearly, sorry.

Author

Commented:
My last comment was the conclusion of my initial question and suited my needs.
Top Expert 2007

Commented:
If all you want to do is read a textfile, use the FileSystemObject and read the file directly - no need for INCLUDE statements, variables or any other crap.

Visit http://www.4guysfromrolla.com/webtech/010401-1.shtml for more info on other ways to read text from a file.
''
'' index.asp:
''
<% @LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Option Explicit
''
Const cREAD = 1
Const cWRITE = 2
Const cAPPEND = 8

Dim fs
Dim ts

Set fs = Server.CreateObject("Scripting.FileSystemObject")

Function read_file(name)
    
    read_file = ""
    
    If fs.FileExists(name) Then
        Set ts = fs.OpenTextFile(name, cREAD)
        read_file = ts.ReadAll
        ts.Close
        Set ts = Nothing
    End If
''
End Function
''
Response.Write read_file("hello.asp") & "<br />"
Response.Write read_file("goodbye.asp") & "<br />"
%>
''
'' Output:
'' hello!
'' goodbye!
''

Open in new window

Top Expert 2007

Commented:
Oh, and no worries - glad to help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial