Lou Garczynski
Lou Garczynski

Reputation: 635

How to include helper functions in multiple classes

I have two major classes, including about 7 files each, each containing 2 to 10 functions.

However, I have a utils.as file, containing about 10 simple explicit functions, that I wish to include in both.

I want to return a code as clean as possible, but I'm not sure what would be the best practice here.

Upvotes: 0

Views: 95

Answers (1)

Stefan van de Vooren
Stefan van de Vooren

Reputation: 2717

Just use public static functions? For example the utils:

package myPackage.utils
{
 public class StringUtil
 {
  public static function substitute (msg : String, ...rest) : String
  {
  // replace all of the parameters in the msg string
  for (var i:int = 0; i < rest.length; i++)
  {
   msg = msg.replace(new RegExp("\\{"+i+"\\}", "g"), rest[i]);
  }
  return msg;
 }
}
}

A class

package myPackage{ 
import myPackage.utils.StringUtils;
public class myClass{
 public function myClass () {

 // use your Util class 
 var t = StringUtil.substitute("test {0}", "first value");
  trace (t);// output test first value
 } 

}}

Upvotes: 1

Related Questions