ASP series lecture (5) use variables and constants

xiaoxiao2021-03-06  71

Variables are named storage locations in computer memory, which contain data such as numbers or strings. The information contained in the variable is called the value of the variable. Variables Use the user to understand the name of the script operation to provide a user with a way to store, retrieve, and operational data. Declaration and name variable naming and declaration variables should follow rules and guidances for scripting languages. Even if you don't need to declare the variables before using the variable, it should also develop a good habit of declaring the variables during programming, as this helps prevent errors. Declaring a variable means telling the script engine, there is a variable of a specific name so that the variable can be referenced in the script. VBScript VBScript does not require a declared variable, but declare them before using all variables, they are a good script writing habit. To declare a variable in VBScript, use the DIM, PUBLIC, or PRIVATE statement. For example: <% DIM UserName%> You can use the VBScript Option Explicit statement in the .asp file that needs to be explicitly declared. Option Explicit must appear before any ASP instruction and any HTML text or script command. This statement only affects the ASP command written with VBScript without affecting the jscript command. JScript Microsoft JScript needs to be declared when the variable is a local process variable, but declares that they are a good script writing habit before using all variables. To declare a variable, use a VAR statement. For example: <% var username;%> The scope of the variable scope variable is a life-threatening period, which is determined which script commands can access variables. The variables declared in the process have a local scope. Each time a process is performed, the variable is created and done. Any command outside the process cannot access it. The variables outside the process have a global scope that can be accessed and modified by any script commands on the ASP page. When declaring variables, local variables and global variables can have the same name. Alternatively, the value of one will not change the other value. If there is no declaration, you may not carefully change the value of a global variable. For example, the following script command returns a value of 1, although there are two names Y variables: <% DIM YY = 1 Call setLocalVariable response.write y SUB setLocalVariable Dim yy = 2 End sub%> Due to the variable declaration, the following scripts The command will return 2. When the process call is set to 2, the script engine believes that the process is to modify the global variable: <% y = 1 Call setLocalVariable response.write y sub setLocalVariable y = 2 End Sub%> Developing explicit statement All variables Habits can avoid many problems. Especially when you use a #include statement to include the file to the ASP home page, it is more important. Scripts included in a separate file are treated as part of the entire file containing its files. Use different names to name the main script and the variables used in the script, which is easily forgotten unless the variable is declared. Giving a session or application scope for variables The global variable is available only in a single ASP page, which must give a session or application scope for variables outside of a single ASP page. The session scope variable is available to all pages in the ASP application requested by the user. The application scope variable is true. For a single user, the session variable is the best way to store information, for example, preferences, usernames, or user IDs. For all users of a special application, the application scope is the best way to store information, such as the initial value required for application-specific greetings or applications.

The ASP provides two built-in objects to allow you to store variables: Session objects and Application objects. You can also create an object instance with a session or application scope. The session scope must give the variable to the session scope, store the variable into the session object, the method is to assign a value for the named entry of the object. For example, the following command stores two new variables into the Session object. <% Session ("firstname") = "jeff") = "smith"%> To retrieve information in the session object, you can use the output command (<% =) or Response.Write to access the named entry. The following example displays the current value of the session ("firstname"): Welcome <% = session ("firstname")%> You can store user preferences in the session object, then you can access these preferences, which is decided to One page is returned to the user. For example, you can allow users to specify plain text content in the first page of the application, and then apply this option to all subsequent pages that the user accessed in the application. <% IF session ("ScreenResolution" = "Low" the%> this is the text version of the page. <% ELSE%> this is the multimedia version of the page. <% End if%> application scope To the application scope to the variable, store the variable to the Application object, the method is to assign a value for the named entry of the object. For example, the following command stores an application-specific greeting in the Application object: <% Application ("Greeting" = "Welcome to Exploration Air"%> To retrieve information from the Application object, you can use ASP output instructions (< % =) Or response.write Access the named entry from any subsequent page of the application. The following example is displayed with an output command: <% = Application ("Greeting")%> Using constant constants is used instead of a number or string name. Some basic components provided with the ASP, such as ActiveX Data Objects (ADO, define constants you can use in the script. Components can declare constant in a component type library, and component type libraries are files that contain information supported by ActiveX components. Once a type library is declared in the Global.asa file, you can use the defined constant in any page of the application. You can use to mark the application declared type library in Global.asa. For example, to declare the ADO type library, you can use the following statement:

转载请注明原文地址:https://www.9cbs.com/read-121260.html

New Post(0)