using data n.
Skip this Video
Loading SlideShow in 5 Seconds..
Using Data PowerPoint Presentation

Using Data

73 Views Download Presentation
Download Presentation

Using Data

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Using Data Active Server Pages

  2. Objectives • In this chapter, you will: • Learn about variables and constants • Explore application and session variables • Learn the use of data types and variable declarations • Learn the role that variables and constants have in an application • Learn the benefits and types of modules • Learn how to create objects from classes • Learn about ASP’s object model

  3. Variables and Constants • A constant is a memory location that cannot change throughout the life of a program • A variable is a memory location that stores values that can change throughout the life of a program • To use a variable in your code, you must give it a name • Variable names in VBScript are not case sensitive

  4. Variables and Constants • In VBScript, variables names must follow these rules: • Must begin with an alphabetic character • Cannot contain an embedded period • Cannot exceed 255 characters • Must be unique in the scope in which they are declared • Cannot be a reserved word (see for a complete listing of reserved words

  5. Variables and Constants • Variable names in JavaScript must also follow some standard rules: • Must begin with a letter (either uppercase or lowercase), an underscore (_), or a dollar sign. All subsequent characters can be letters, numbers, underscores, or dollar signs • Cannot be a reserved word

  6. Application and Session Variables • Active server pages are dynamic • When an ASP is processed, its variables are active only while the response page is being generated by the ASP script • You can make data available after the response pages generate; you do this by using special variables • The Application variable can be used by everyone who accesses the application • Session variables can only be used by the user who is logged on to the application during that session

  7. Data Types • Variables contain data; data comes in data types • The data type indicates characteristics of the contained data • Different programming languages allow for different data types • VBScript allows for only one data type, called a variant • A variant can store different types of data at different times

  8. Data Types and Their Three-Character Prefixes

  9. The Scope of a Variable • A variable’s scopespecifies when the application does and does not have access to the variable • You indicate the scope that you want a variable to have by declaring the variable in a specific place, such as: • Application object • Session object • Script • Procedure

  10. Declaring Variables • Once you decide to use a variable or constant and then pick its data type, the next step is to declare the variable • A variable declaration is a line of code that tells the program what variables you will use • In VBScript, a typical variable declaration includes the following: Dim myVar • A typical variable declaration in JavaScript is as follows: var Myvar; • While VBScript do not require it, you should declare all variables at the beginning of the script

  11. 2 Declaring Constants • The next example shows how to declare constants in VBScript • JavaScript does not support constants; therefore, in JavaScript, you need to use a regular variable, set to a specific value in place of a constant Const myConst = 2

  12. 2 Declaring Application and Session Variables • You declare an Application or Session variable with an assignment statement, which allows you to give a variable a value • VBScript and JavaScript declare variables in this manner: Application (“myVar”) = 0 Or Application (“myVar”) = “This is a string” Application (“myVar”) = 0; Or Application (“myVar”) = “This is a string”;

  13. Using Variables and Constants in an Application • Now that you have a good grounding in variables and constants, it’s time to take a look at them within an application

  14. 2 The Inches to Feet Program

  15. 2 After the Conversion

  16. Creating Modules • A module is a series of steps designed to perform a single task • Modularization is the dividing of an application into two parts • There are two different kinds of modules: • Subroutines (also known as procedures) • Functions • Both allow you to reuse a segment of code repeatedly in your programs

  17. Subroutines or Procedures • A subroutine is a series of steps that accomplishes a task but does not usually return a value • There are two steps involved in creating a subroutine: • Defining – Calling • The following shows how to define a subroutine in VBScript: SUB subroutine_name • • • [Code goes here] END SUB

  18. Subroutines or Procedures • A module in JavaScript looks a little different from VBScript: function function_name( ) { [CODE GOES HERE] } [END CODE]

  19. Subroutines or Procedures • In JavaScript, a subroutine begins with the keyword FUNCTION followed by the name of the function • A subroutine call in JavaScript consists of the name of the subroutine followed by parentheses and a semicolon

  20. Creating a VBScript Subroutine • We are going to use a VBScript subroutine in the conversion application in this chapter • Then, we will investigate the JavaScript version of the same code • To modify the calc_feet.asp file in VBScript use the steps on pages 32 to 34 in the textbook

  21. Calling inches_to_feet

  22. Reviewing a JavaScript Subroutine • If you wanted to accomplish the same task with JavaScript, you would use the following code • (Note that the bolded sections reflect the differences between the JavaScript and VBScript versions, and the “…” was used to indicate code that was unchanged and thus deleted for brevity) • Refer to the Code Example on pages 34 and 35 in the handouts

  23. Functions • A function accomplishes a series of steps and returns a single value • The code on page 35 in the handout shows a VBScript function that adds two numbers • The two numbers passed into the function are called arguments • An argument is a variable used to do calculations in the function itself • A function can have zero or more arguments, separated by commas

  24. Calling a Function • The following, written in VBScript, calls a function: Dim result result = Add (1, 2) • To do the same in JavaScript, you would write the following code: var result; result = Add (1, 2);

  25. Adding a VBScript Function to calc_feet.asp • In the following steps, you will add a function to the calc_feet.asp page in VBScript • This function will be responsible for converting inches into feet • Follow the directions on pages 36 and 37 of the textbook to add a function to the calc_feet.asp page in VBScript

  26. Adding a VBScript Function to calc_feet.asp • Code Dissection • The first bold line of code is the function definition: FUNCTION convert_to_feet(intinches) • The first block of bold code includes the FUNCTION…END FUNCTION keywords • In the first block of bold code, the line that begins with convert_to_feet=round(intinches/12,1) is the actual function • The last bold line calls the function

  27. Request a Function in JavaScript • In JavaScript, a function uses the keyword “return” to return a value from the function • The code on page 38 in the handoutshows the JavaScript version of the previous function example • Code Dissection • The first block of bold code shows how to create a function in JavaScript • The second block of code shows how to call a function in JavaScript

  28. Creating Objects from Classes • A class defines the properties and methods of the objects that reside in it • Once you create a class, you can use it to create multiple objects • You can create classes in VBScript or in JavaScript

  29. Classes in VBScript • Within VBScript, you declare properties and methods associated with a class inside the CLASS…END CLASS keywords • You name a class by using the three letter prefix “cls” and the name of your choice • To create a class in VBScript perform the instructions listed on pages 39 and 40 in the handouts

  30. Classes in JavaScript • To create a class in JavaScript, you need to create a function that creates the class (which is an object itself) and that declares the properties and methods to be given to any subsequent objects from that class • The function name should be the name of the class, which is clsConversion in the example on pages 40 and 41 in the handout

  31. Declaring an Object from a Class • The next step after you create a class in VBScript or JavaScript, is to declare an object that will be based on the class • Declaring an object from a class is called creating an instance of the class • Code Example The following shows how to declare an object from a VBScript class: Dim objConversion Set objConversion = new clsConversion The following shows how to declare an object from a JavaScript class: varobjConversion; objConversion = new clsConversion( );

  32. Using Classes in VBScript • The running example in the handouts converts inches to feet • The use of classes allows the developer of the class to hide from the programmer the details of how the conversion is done • The use of a class makes it possible for the programmer to create an application object without actually having to learn how to do the conversion itself • To include the clsConversion.cls class in his or her script, the programmer would use the following line: <!-- #include file=“clsConversion.cls”-->

  33. Using Classes in VBScript • The code did not specify where clsConversion.cls is located • The class file you just created is called a Server-Side Include (SSI) file • Sometimes for management reasons, you will want your shared script files to reside in their own directories

  34. Using Classes in VBScript • To do this, you will need to use a virtualpath, which is a directory path that assumes that the Web application’s root folder is the virtual directory created for this application • To modify the calc_fee.asp to use the clsConversion class, refer to the processes on pages 43 and 44 in the handouts

  35. Using ASP’s Object Model • You have encountered brief mentions of the Application, Session, Request, and Response objects • It is now time to review them in detail so that you understand properties, methods, and events that are available to you for use in future chapters

  36. Application Object • The Application object, which initializes server variables that are used throughout the life of a Web application, supplies the necessary interface you need to create Web applications

  37. Session Object • The Session object, which initializes a user’s session inside a Web application, provides a mechanism for creating server variables and firing common events related to a user’s session

  38. Request Object • The Request object supplies information regarding the request the user sends to the Web application

  39. Response Object • The Response object provides a way of sending a response message to the client in HTML

  40. Summary • Data exists in a program as a variable or as a constant • A constant is a memory location that cannot change throughout the life of a program • A variable is a memory location that stores values that can change throughout the life of a program • Variables contain data; data comes in data types • A variable declaration is a line of code that tells the program what variables you will use

  41. Summary • A module is a series of steps designed to perform a single task • You can create objects efficiently by using a class • The Application, Sessions, Request, and Response objects are common in ASP • Each has its own unique combination of available properties, methods, and events