Home
Tools/Projects
ColdFusion
JavaScript
Articles/Tutorials
Creating Dynamic "onload" Events Using JavaScript
Client-to-Server Communication Using DHTML
Executing a DTS Package from CF/ASP/PHP/SQL
Creating a Custom ColdFusion Tag (101)
Structures
Downloads
Custom Tags
qForms JSAPI
Clients
About

Structures: Advanced Use of CF-Based Structures
by Dan G. Switzer, II | 10/12/1999

We've covered the basics and some very pratical ways to use structures, now let's show how far structures can really be taken. In this portion of the tutorial, we'll cover how to nest structures in your applications.

The first thing we need to do, is to initialize our structures for the example. We'll need three structures. One for temporary storage, one to contain all the structures and the last to hold only the current structure needed.

Source Code

Now we're going to create several different shapes all with the same properties; a basketball, a volleyball and a baseball.

Let's start by creating the basketball structure:

Source Code

Now that we have created our temporary structure, we want to make a copy into the "stBalls" structure. We're going to make copies of all the different types of balls and store the results in one structure. This will make it easy to retrieve the properties of the ball we need to minipulate.

Source Code

Since we've copied the temporary structures content into our main structure, we now need to clear the values contained in our temporary structure so we can begin to populate it with new information.

Source Code

Next, let's create the structures for both the volleyball and the baseball.

Source Code

We now have a main structure which contains a keys for all of the different types of balls we now about. If we need to know the size of any type of ball, we simply call the structure using the name of the ball and request its "Size" key. For example:

Source Code
Output

Once we've created all of our different types of balls, the next logical step it to have it interact with user input. Let's say we have a form that allows the user to select a type of ball. Once the user selects the type of ball they want to view, we need to output everything we know about the ball. Let's see what our form will look like first.

Source Code

Next let's look at how the we could use the structure we built above to interface with the information passed in the form.

Let's store the request ball information into our the structure we reserved for the current requested information. We'll use a pointer to the structure to save on memory used. The main benefit of using the pointer in this case is to cut down on the number of evaluations—not to mention it makes the code a little easier to read.

Source Code

Now that we've created the pointer to the information we need, let's loop over the keys in that structure and output them to the screen.

Source Code

To see all of this in action, check out Example 2.

You should now have a good feel of how you can start to incorporate structures into your CF applications. Methods used in Part 3 can be extremely useful when storing your main structures in a persistent enviroment—like in the SERVER or APPLICATION scope. This allows you to define all the information once, then reference just the required structures locally.

  [< Previous ]    

If you have any questions about this article, please feel free to contact the author at dswitzer@pengoworks.com.

Copyright © 2024, PengoWorks.com. All Rights Reserved.