Wednesday, March 31, 2004

Event handling in Flash ActionScript 

Understanding the scope of variables, clips, etc. in Flash can be daunting. One instance that is particularly problematic is inside functions that are assigned as event handlers. It is problematic because inside the function you can't access external variables without using their absolute path. This is not convenient when build ActionScript classes.

To get around this you can create properties on the event handler which point to existing objects whose properties and methods may be useful within the handling function.

For instance, here is a function that calls a web service. The web service has an onResult event which fires when the response is received and parsed. However, the class which contains this function is not scoped inside the function that handles the onResult from the web service! In order to get around this, a parent property is assigned a reference to the instance of the class that contains the web service function.

//############################################################################
//A class contains this method which calls a web service
private function initSourceContext():Void {
var wsc:WebService = new WebService(_webServiceUrl);
//Call the method on the WebService
var res = wsc.getLocalID();
//Assign this instance of the class to a new property on res result object
//so it is accessible in the onResult event
res.parent = this;
//The onResult event fires when the WebService response data is
//received and parsed
res.onResult = function(result) {
//Assign the result to a variable defined in the "parent" class
res.parent._sourceContext = result;
//Broadcast an event on the "parent" class
res.parent.raiseEvent("onWebServiceCalled");
};
//The onFault fires if there was any trouble with the web service
res.onFault = function(fault) {
trace(fault.faultCode + "," + fault.faultstring);
}
}

Using the Tilde in URLs 

One great feature about ASP.NET over classic ASP is the introduction of the Tilde for specifying directory paths. Instead of being limited to

absolute
"/mydir/includes/include.asp"
PROS: Same link works everywhere in the site.

and relative
"../../includes/include.asp"
PROS: Supports relative placement so sections of site can be moved across subdirectories without breaking the code.

Now there is the tilde which refers to the root of the current virtual directory.
"~/includes/include.asp"
PROS: Same link works everywhere in the virtual directory
AND: The application can be moved to a different virtual directory without breaking the code.

Tuesday, March 16, 2004

Generate a dot net key pair 

Use the following command to generate a key pair for signing assemblies and miscellaneous other things like assigning assembly permissions in the Microsoft .NET Framework Configuration.

sn -k MyFullKey.snk

This is where the Strong Name Tool was located on my computer:
C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin\sn.exe

The Strong Name Tool has long list of options for extracting public keys, comparing assemblies, verifying signatures, etc. sn -h or sn -? will display the help.

Thursday, March 11, 2004

ADO.NET performance tuning 

I was debugging a long-running page and thought the SQL was too complex. I checked the SP run time, and it seemed OK. I then started getting some error messages about timeouts and the connection pool being exhausted. The problem was that I was not closing the DataReader. I usually wouldn't even notice the problem (the reader is eventually closed anyway), but the statement was in a loop, so it was evident quite quickly.

I went through the code and made sure every ".Read()" was either in a using() statement or followed by a .Close. The page performance improved quite a bit (from timing out to less than 2 sec!), and the other pages are faster, too.

Explicit XSL stylesheet reference in an XML file 

To include an XSL file in an XML file use the following syntax:

<?xml-stylesheet type="text/xsl" href="file.xsl"?>

It seems simple enough, but I am always forgetting how to do it. The nice thing about Internet Explorer is that it will automatically transform the XML document that contains the above tag using the referenced XSL file.

Flash ActionScript compile error 

If you open an older FLA file that uses ActionScript 1 and begin rewriting it in ActionScript 2.0, change the Publish Settings of the FLA file to ActionScript 2.0. If you don't do so, your FLA file will not compile correctly, but no errors will be generated.

This page is powered by Blogger. Isn't yours?