Is there a way for UnDim Variable?

I'm just wondering if it's possible to disable a variable.

Imagine this is my #include file that I use on an ASP page

Dim MyVar MyVar = "Hello World" Response.write(MyVar) 'From now on I can not use Dim MyVar anymore as it throws an error 'I have tried MyVar = Nothing Set MyVar = Nothing 'But again, when you do Dim MyVar 'It throws an error. 

The reason for this is that I cannot use the same #INCLUDE file more than once per page. And yes, I like to use Option Explicit as it helps me clear my code.

*) Edited: I see that it is not as clear as I wanted.

Imagine this is "include.asp"

 <% Dim A A=1 Response.Cookie("beer")=A %> 

Now the asp page:

 <!--#include file="include.asp"--> <% 'Now: I do not see whats in include above and I want to use variable A Dim A 'And I get an error 'I also cannot use the same include again: %> <!--#include file="include.asp"--> 

Do you see my thought? If I could UNDIM the variable A at the end of Include, the problem would be solved.

0
source share
2 answers

There is no option for the UnDim variable. Fortunately, you do not need this either.

Whenever you try to declare a variable twice in the same scope, you are already making a mistake. Find it useful that the runtime does not allow you.

Decision:

  • Do not work with global variables. Use functions, declare your variables there.
  • Do not include the same file more than once.
+6
source

I agree with Tomalak - I'm really not sure why you (or need) to include the same file twice (?)

This seems to be a bad design philosophy, it might be better to encapsulate the subroutines in the include file as functions or subroutines that you can name — no need to include it twice.

Also, while you cannot calm down, you can refuse, but I do not want to encourage bad practices, given what you seem to want to do.

Instead, you can use something like this:

Include.asp:

  <% Function SetCookie(scVar, scVal) Response.cookie (scVar) = scVal End Function %> 

Asp page:

  <!--#include file="include.asp"--> <% Dim A A=1 SetCookie "Beer", A A=1 ' This is kind of redundant in this code. SetCookie "Beer", A %> 

However, if you want to use global variables and are saved with inclusion twice, you can do it this way by adding another include for global variables.

Globals.asp:

  <% Dim globalVarA ...other global stuff here.... %> 

Include.asp:

 <% globalVarA=1 Response.Cookie("beer")=globalVarA %> 

Now the asp page:

 <!--#include file="globals.asp"--> <!--#include file="include.asp"--> <% Dim A A=....something...... %> <!--#include file="include.asp"--> 
+1
source

Source: https://habr.com/ru/post/1262370/


All Articles