Structures: Advanced Use of CF-Based Structures
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.
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:
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.
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.
Next, let's create the structures for both the volleyball and the baseball.
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:
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.
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.
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.
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 firstname.lastname@example.org.
|Copyright © 2013, PengoWorks.com. All Rights Reserved.|