Avatar of GrrWolfie
GrrWolfie
Flag for United States of America asked on

How to "undim" a variable

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!
ASPASP.NETVB Script

Avatar of undefined
Last Comment
Badotz

8/22/2022 - Mon
SOLUTION
Qlemo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
GrrWolfie

ASKER
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

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.
Andrei Fomitchev

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
SOLUTION
Badotz

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
GrrWolfie

ASKER
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.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Badotz

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

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

ASKER
My last comment was the conclusion of my initial question and suited my needs.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Badotz

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

Badotz

Oh, and no worries - glad to help.