ColdFusion MX Datatypes


In ColdFusion MX, datatypes can be categorized as either simple or complex. Simple datatypes include strings, numbers (such as integer and floating point), date/time, and Boolean values. Complex datatypes include arrays, lists, and structures.

Simple Datatypes

The following sections discuss the simple datatypes, which are the basic datatypes such as strings, numbers, and Boolean values.

String

The String datatype stores text values that can contain alphanumeric characters, spaces, and special characters such as *, &, %, and @. ColdFusion allows you to assign a string to a variable. The length of the string is limited only by the available computer memory. When you assign a string to a variable, you need to include the text either in single or double quotes. Quotes are used to indicate that the value assigned is of the String datatype. A few examples of the string datatype are

  • "John Williams"

  • "The sales of the book have increased by 50%"

  • "Andrew & John"

  • "200"

The last value in the preceding list is a number. However, because the value has been enclosed within quotes, ColdFusion considers it a String value.

Note

Escaping enables ColdFusion to ignore the special meaning of a reserved character, such as double and single quotes, when it's enclosed within a string. For example, when used inside a <CFOUPUT> tag, the pound (#) sign must be escaped. Escaping allows the pound sign to be used as a display character instead of a character to identify a variable.

Integers and Floating-Point Numbers

An integer datatype stores whole numbers ranging from 2,147,483,648 to 2,147,483,647. When you assign a value greater than 2,147,483,647 or less than 2,147,483,648, ColdFusion assigns the real number datatype to the variable.

Floating-point numbers store decimal values in the range 10300, or 1 with 300 zeros after it. You can also store floating-point values in scientific notations, such as in the form 3 E 16. Calculations done by using floating-point numbers are accurate to 12 digits of precision.

Date/Time

A date/time datatype stores date and time values in the range of 100 AD to 9999 AD. If you assign only a date to a date/time type variable, by default the time is set to 12:00 a.m. Similar to strings, you need to include the date and time values in double quotes. ColdFusion supports various date and time formats:

  • "February 18, 2002"

  • "Feb 18, 2002"

  • "02/18/2002"

  • "02/18/02"

  • "2002-8-02"

  • "02-02-18"

The various formats in which you can specify time are

  • "02:55am"

  • "02:55am"

  • "2:55am"

  • "2am"

  • "02:55:30"

When you specify dates with two-digit years, ColdFusion interprets years from 00 to 29 as 21st-century years. This means that when you specify a date as "02/18/02", ColdFusion interprets the year to be 2002. Years from 30 to 99 are interpreted as 20th-century years. Therefore, when you specify a date as "02/18/77", ColdFusion identifies the year as 1977.

Boolean

A Boolean value is either TRUE or FALSE. It's the outcome of a logical operation. The numeric equivalent of TRUE is 1 and of FALSE is 0. In the string form, Boolean values are set to Yes for TRUE and No for FALSE. ColdFusion is not case sensitive. Therefore, True is the same as TRUE and true.

Complex Datatypes

The preceding section discussed the simple datatypes. In this section, you'll learn about complex datatypes, such as arrays. These are amenable to more complex operations than the simple datatypes, such as strings.

Arrays

An array stores a collection of variables of the same datatype. An array can be one-dimensional, two-dimensional, or multidimensional. Unlike traditional arrays, which are symmetrical and of fixed size, ColdFusion arrays are dynamic in nature. They can have different lengths and can expand or contract when elements are added or deleted.

The syntax to create an array is

 <CFSET sampleArray=ArrayNew(x)> 

Here, x specifies the dimensions of the array declared. The dimensions can be 1, 2, or 3.

You can increase the dimensions of an array by nesting its elements. For example, to declare a one-dimensional array, OneDArray, you use the ArrayNew function. Similarly, to declare a two-dimensional array, TwoDArray, and for a three-dimensional array, ThreeDArray the ArrayNew function is used. This is shown in the following syntax:

 <CFSET OneDArray=ArrayNew(1)> <CFSET TwoDArray=ArrayNew(2)> <CFSET ThreeDArray=ArrayNew(3)> 

Use this syntax to create a multidimensional array:

 <CFSET ThreeDArray [1][1][1] = OneDArray> <CFSET ThreeDArray [2][1][1] = TwoDArray> 

In this syntax, the multidimensional array named ThreeDArray has been nested in the single-dimensional array named OneDArray. A multidimensional array can have up to three dimensions.

After you create an array, you need to assign the elements. For example, to assign elements to a single-dimension array, EmpName, use the following syntax:

 <CFSET EmpName=ArrayNew (1)> <CFSET EmpName[1]="Albert"> <CFSET EmpName[2]="Mary"> <CFSET EmpName[3]="James"> 

Lists

A list is a special type of string that contains delimited elements. Consider the following list: "b,c,n,q". In the list, "b", "c", "n", and "q" are the elements, delimited by commas. A list is considered a single item, and the comma is the default delimiter.

Structures

In ColdFusion applications, structures are used to store key-value pairs. A key-value pair is a specific key of the structure with its value. For example, a structure called Name has two keys, FName and LName, and its values are Albert and Lucy. In this case, one key-value pair is FName-Albert.

A structure helps build a collection of related variables that are grouped under the same name. You can create structures dynamically.

Structures are similar to one-dimensional arrays. Unlike one-dimensional arrays, however, a key references each element of the structure. This key needs to be a string. Structures are commonly known as associative arrays or hashes.

Structures refer to related values as a unit rather than individually. For example, to maintain an employee list, you create a structure that maintains personal information, such as FName, LName, Address, Phone Number, and Employee ID. You refer to this collection of information as a structure called Employee, rather than as a collection of individual variables. The values that are associated with this key can be a string, an integer, or another structure.

You can create structures by assigning a variable name to the structure using the StructNew function. To create a structure named Employee, use this syntax:

 <CFSET Employee=StructNew()> 

This syntax creates a structure named Employee into which you can insert data.

To add elements to a structure, use the StructInsert function. The syntax is as follows:

 <CFSET value=StructInsert (structure_name, key, value, [, AllowOverwrite])> 

To add the keys Username and Password with the values John and Matthew to the structure Employee, use the following syntax:

 <CFSET rc=StructInsert (User, Username, John) <CFSET rc=StructInsert (User, Password, Matthew) 

In this code, the structure is declared using the StructNew() function. Next, the values John and Matthew are inserted by using the StructInsert() function. Finally, the <CFOUTPUT> tag is used to display the values.




Macromedia ColdFusion MX. Professional Projects
ColdFusion MX Professional Projects
ISBN: 1592000126
EAN: 2147483647
Year: 2002
Pages: 200

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net