18
Jan
07

ActionScript Example: Creating a Static Function

So a developer I work with was going to use an include file and a prototype for a string altering method he wanted to use throughout his application. The only thing that came to mind was the Mr. Horses catch phrase from Ren & Stimpy.

“No sir I don’t like it”.

I offered the possibility that he might create a utilites class in our namespace with a static function to accomplish his goal. To that he asked “Can you give me an example of how to make a static function”. Well here it is:

First we have our class:

class com.imart.utilities.StringHelper{

function StringHelper(){

}
public static function returnString(passValue:String):String{
return “Here you go:” + passValue;
}
}

Then we can call the method:

import com.imart.utilities.*

example_txt.text = StringHelper.returnString(”Steve”);

Download the example project


1 Response to “ActionScript Example: Creating a Static Function”


  1. 1 Feb 25th, 2007 at 5:57 am

    Thanks for the example, I was over at livedocs trying to grasp the static member concept, and I can see the application (in many of my own programs) for it, I wonder though, is there an advantage to using static functions in a situation where one or multiple instances of a class are instantiated? Understanding your example helps me to realize the power of writing classes with purely static members…thanks for this.

    Aaron


Vote

flickr