Avatar billede mikmakmuk Nybegynder
18. april 2002 - 10:32 Der er 5 kommentarer og
1 løsning

Lokale variabler

Kan man gøre variable lokale for en specifik funktion?
Avatar billede puppetmaster Nybegynder
18. april 2002 - 10:36 #1
Whether you just started to learn ASP or you are an ASP expert, you've definitely dealt with both Local and Global variables at one point or another. For the benefit of those who are learning ASP, we'll go ahead and explain what Local and Global variables are; for those who are ASP experts, this will be a refresher. Let's get started.

Keep the following in mind: If you assign a value to a variable and then assign another value to the same variable, then the old value will be replaced with the new one - that is not the case with Local variables.

Local Variables

Local variables are variables that exist only during the lifetime of the procedure that created them. In other words, once a procedure has stopped running (has finished what it was supposed to do), the variable that was created at the beginning of the procedure ceases to exist therefore you can assign different values to the same variable. Let's see an example of how they work (variables are in green) - this script will go in the BODY of your HTML document.

<% Sub Procedure_1 ( )
strText = "'This is a text string"%>
<%=strText%>
<%End Sub%>

<% Sub Procedure_2 ( )
strText = "This is another text string"%>
<%=strText%>
<%End Sub%>


<p>This is the result for the first procedure: <%Procedure_1( )%></p>
<p>This is the result for the second procedure: <%Procedure_2 ( )%></p>
<p>This is the result for the first procedure: <%Procedure_1( )%>, again."%>

See it in action


As you can see, strText ceased to exist once the first procedure finished executing thus you could assign a new value to the same variable in the second procedure. The "old" value of the variable could be called again if the first procedure is once again invoked - as it is shown in the example.
Avatar billede puppetmaster Nybegynder
18. april 2002 - 10:36 #2
Global Variables

Global variables unlike Local Variables, persist throughout the lifetime of the script, not a procedure. In other words, they last for as long as the script is running. Let's see how they work.

<%strGlobal = "Global variables persist during the lifetime of the script."%>
<%=strGlobal%>

<% Sub Procedure_1 ( )
strText = "'This is a text string"%>
<%=strText%>
<%=strGlobal%>
<%End Sub%>

<% Sub Procedure_2 ( )
strText = "This is another text string"%>
<%=strText%>
<%=strGlobal%>
<%End Sub%>


<p>This is the result for the first procedure: <%Procedure_1( )%></p>
<p>This is the result for the second procedure: <%Procedure_2 ( )%></p>
<p>This is the result for the first procedure: <%Procedure_1( )%>, again."%>
Avatar billede puppetmaster Nybegynder
18. april 2002 - 10:42 #4
Use local variables, and avoid public variables. Local variables can be accessed quicker by the ASP scripting engine than public variables, because the entire namespace doesn't have to be searched.
Avatar billede puppetmaster Nybegynder
18. april 2002 - 10:51 #5
Takker!
Avatar billede mikmakmuk Nybegynder
18. april 2002 - 10:56 #6
Det er for fedt at der findes folk som dig, som gider hjælpe folk som mig. Hvis du forstår hvad jeg mener :-)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester