Variables and Conditions Nicholas Tunney Senior Software Architect
Variables and Conditions Nicholas Tunney Senior Software Architect
Why Am I Listening to This Guy? • Experience • Credentials – – – Articles and Whitepapers Presentations Adobe Community Expert Adobe Certified Advanced Cold. Fusion Developer Adobe Certified Instructor • Current Projects
What will we cover? • • Variable Basics Types of Variables Cold. Fusion Variable Scopes Conditional Logic
What is a variable? • • Container Name/value pair Dynamic (changing) Variables permit our programs to work with values that can't be known at design time or that may change from their original value
Data typing • Different data types hold different types of values – String: “Hello World” – Int: 42 – Structure: Name Nic Tunney Presenter True Age 28 get. Model() Return string get. Make() Return string – Object (Car) accelerate()
Strong data typing • Must declare a variable’s type before it can be used String my. Name; Int my. Age; Company my. Employer; • Strongly typed languages – Java – C#
Weak data typing • Language determines type of variable at runtime dynamically • Weakly typed languages are NOT untyped languages • Weakly typed languages – Smalltalk – Cold. Fusion
Cold. Fusion data types • Simple: – Strings (lists) – Integers – Real numbers – Boolean values – Date-time values
Cold. Fusion data types • Binary – Raw data such as images • Object – COM – CORBA – Java – Web Services – Cold. Fusion Components
Cold. Fusion data types • Complex: – Arrays – Structures – Queries
Cold. Fusion Arrays • Array: – A set of sequentially indexed elements having the same type of data. Each element of an array has a unique identifying index number. Changes made to one element of an array do not affect the other elements. • Array Element – Individual item stored within an array
One Dimensional Arrays • Simple ordered stack of values my. Array 1 Nic 2 Kelly 3 Simon • Addressable – Refer to each element using [x] my. Array[1], my. Array[2]
Two Dimensional Arrays • Collection of ordered values my. Array 1 2 1 Nic Tunney 2 Simon Horwith • Addressable – Refer to each element as [x][y] my. Array[1][1], my. Array[2][2]
Cold. Fusion Structures • A container (variable) that’s holds a group of data objects. Unlike arrays, each data object in a structure generally is of a different type • Structures are unordered my. Struct speaker Nic Tunney topic Variables is. Active true • Addressable my. Struct. speaker, my. Struct[“topic”]
Cold. Fusion Queries • Series of 1 or more rows with column or field values for each row my. Query id price description 001 29. 99 Widget 002 59. 99 Foobar • Addressable products. price – refers to current row products. price[2] – specifies row
Objects • Objects are fundamentally different from all other data types in that they encapsulate both data and behavior • Objects are real world representations Car • get. Make(): string • add. Gas(gallons): void • accelerate(): void
Cold. Fusion Scopes • A variable typically has a lifespan and a scope – lifespan: how long the system keeps a reference to a variable – scope: location from which variable is visible • In Cold. Fusion, a variable's scope and lifespan are bound together in a single attribute, scope, which is prefixed to the variable name
Cold. Fusion Scopes prefix visibility prefix required lifespan application everywhere in same app Yes life of application attributes within custom tag (for variables passed into custom tag) Yes current request caller within custom tag (for variables on calling page) Yes current request cgi everywhere No current request client everywhere in same application for an individual user No varies by purge timeframe cookie everywhere in all apps on domain No varies; set by cookie form everywhere No current request everywhere Yes current request server everywhere by all apps on single server Yes varies by server timeout setting session everywhere in same app for an individual user Yes varies by session timeout setting url everywhere No current request variables local to page/CFC and any included pages (default) No current request
Conditional Expressions • A conditional is an expression that can be evaluated to a boolean value • Certain actions are taken depending on true versus false
<cfif> • Evaluates an expression <cfif expression> Do Something </cfif>
<cfelseif> • Allows us to additional conditions if first condition is false <cfif expression> Do something <cfelseif different_expression> Do something else </cfif>
<cfelse> • Handles the false condition of a <cfif> block • “Catch All” condition <cfif expression> Do something <cfelse> Do something else </cfif>
<cfswitch><cfcase> • Rule of threes • Evaluates expression and passes control to the matching <cfcase> tag <cfswitch expression = "expression"> <cfcase value = "value" delimiters = "delimiters"> HTML and CFML tags </cfcase> … additional <cfcase></cfcase> tags … <cfdefaultcase> HTML and CFML tags </cfdefaultcase> </cfswitch>
Operators Cold. Fusion Equivalent Operator to IS, = EQUALS, EQ LT < LTE, LE <= GT > GTE, GE >= IS NOT, NEQ <>
Conclusion • Variables are dynamic (changing) • Variables give context and constraint to values • Experiment with complex variables to help ease coding and maintenance time • Cold. Fusion is a loosely typed language, but not untyped • Conditionals allow us to execute commands based on the result of some run time boolean value
Conclusion • This presentation laid the ground work for using variables and conditions in Cold. Fusion • Next steps – CF Tags – Functions for complex data types • About. Web Training – Fast Track to Cold. Fusion – Custom Class
Thanks! http: //www. aboutweb. com http: //www. nictunney. com
- Slides: 27