|
Comments
|
Today's Top SOA Links
BF on CF The Object Of It All
The Object Of It All
By: Ben Forta
Jan. 12, 1999 12:00 AM
That's good for us? Yes, most definitely. ASP has no built-in functionality for many simple things that are built into ColdFusion, so ASP developers use COM objects extensively, even for those little things that we CFers take for granted: generating SMTP e-mail messages, for example. To generate e-mail, ASP developers must install third-party components that provide them with that functionality. So why is this good news for us? Well, the good news is that there are hundreds of third-party components out there, offering all sorts of functionality. Some are freebies. Others are shareware or commercial software. And ColdFusion developers can take advantage of them all. This column won't teach you how to write your own COM objects. It will, however, teach you how to use COM objects and how to convert the sample ASP code that accompanies most COM objects into CF code. Note that the examples here use an excellent graphing object, AspChart, that allows you to create professional and flexible charts on the fly in several file formats. AspChart is created by ServerObjects Inc. (www.serverobjects.com). The president of ServerObjects is Stephen Genusa, one of the leading authorities on ASP development.
Understanding COM Objects COM objects are usually DLL or EXE files and can be written in almost any language imaginable, such as C/C++, Visual Basic, Java and Delphi. Once the object is written, it's installed and registered on the computer it will be used on. Registering the COM object publishes it (and its interfaces) to other applications so they can use it. Almost all COM objects come with installation and registration instructions. Those instructions are the same regardless of the application using the COM object.
Instantiating COM Objects To initialize COM objects in Cold Fusion, you use the <CFOBJECT> tag, which takes the name of the object as an attribute and a name that you use to refer to that object once it's been initialized. The code to initialize AspChart looks like this: <CFOBJECT ACTION="CREATE" NAME="Chart" CLASS="ASPChart.Chart"> In this example the <CFOBJECT> tag is creating an instance of the chart object. The object's name is "ASPChart.Chart" (every object has a unique name) and it is passed in the CLASS attribute. <CFOBJECT> instantiates the object, which can now be referred to as "Chart" as specified in the NAME attribute. Just as a point of reference, here's the ASP code that does the same thing: Set Chart=Server.CreateObject("ASPChart.Chart") As you can see, converting the sample ASP instantiation code to ColdFusion code is pretty simple.
Setting Object Properties Two of AspChart's properties are Height and Width. These specify the size (in pixels) of the image to be generated. The ASP code to set these properties is:
Chart.Height=300
<CFSET Chart.Height=300>
Setting Object Subproperties
Chart.ChartTitleFont.Size=20
<CFSET ChartFont=Chart.ChartTitleFont>
Invoking Object Methods Every COM object has at least one method, and some have many more. The code to invoke an object method in ASP looks like this: Chart.SaveChart This invokes the AspChart SaveChart method that actually creates the image file based on all the properties already set. The equivalent ColdFusion code is: <CFSET temp=Chart.SaveChart()> From ColdFusion's perspective, the only difference between setting a property and invoking a method is that methods must have () characters after the method name (methods are essentially functions, and ColdFusion uses () to distinguish functions). Some methods take one or more parameters. To pass parameters to a method in ColdFusion, just specify them in a comma-delimited list between the "(" and ")" characters. That's all there is to it. Pretty simple indeed.
Where to Go from Here For more information about ColdFusion's COM support, the <CFOBJECT> tag and writing COM objects, see the chapter entitled "Interfacing with COM and DCOM Objects" in my book ColdFusion Web Application Construction Kit (ISBN 07897-14140). Reader Feedback: Page 1 of 1
Your Feedback
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
|||||||||||||||||||||||||||||||