Faking constants in Actionscript 2.0

Technology

Most people get around Actionscript 2.0’s lack of constant variables by just naming member variables in all capital letters and remembering not to alter their value. This works okay, assuming you obey the rules, but what if you want to enforce a constant value so that it cannot be changed during runtime. Edgar De Loa wrote to us about this today:

Constants are not inherent in actionscript 2.0. The closest one could get is to make something private, and globally accessible. However, this doesn’t ameliorate the problem, since the variable could still easily be changed, and the notion of it being a constant crushed.

I’ve found a very easy way to simulate constants. After looking around, I’ve found no one else that has suggested and/or used this method.

The first step is to create a static class, and define a private variable with the value one would like to store. The next step is to add a public function that returns the aforementioned private variable. Naming the function identical to the private variable (all in caps), is a great and easy way to access the constant.


static class ConstantVars {
  private var ARRAY_SIZE = 12;

  public function ARRAY_SIZE() {
    return ARRAY_SIZE;
  }
}

So now, one can use ConstantVars.ARRAY_SIZE() to access the constant, with no way to change or get rid of it. I’ve also found that naming the class something simple, like “Const” will make everything even more quick and simple.

This works, but I think we can even do one better.

By using a static “getter” method, you can retrieve enforced constant value and you don’t need to use a function’s normal parenthesis notation to access it. You can use this in a more global static “Const” class as Edgar describes for more global variables. Alternatively, you can encapsulate the static constant in any normal class, which is handy if the constant is relevant to a particular class (instead of just a global const variable).

As an example, let’s say you had a class that authenticates a user. It’s not a static class. It has some member variables and a few methods that deal with authenticating and storing a user’s credentials. It also has a few message strings that are relevant to the authentication process, which you may want available to other parts of the application, but you want to make sure these strings are constants and aren’t accidentally changed at runtime:


class Authentication {
  private var guid;
  private var username;
  private var password;
  private var server;

  public static function get SUCCESS_MSG():String { return "Login successful"; }
  public static function get NOSUCHUSER_MSG():String { return "The user was not found"; }
  public static function get SERVERERROR_MSG():String { return "There was an error while connecting to the server"; }

  public function Authentication( server, username, password ) {
    ...
  }
  public function authenticate():String {
    //Performs the authentication and returns one of the constant error strings
  }
  ...
}

Now code inside your Authentication class can refer to NOSUCHUSER_MSG directly, and code external to the class can access the constant values in a simple manner like Authentication.SERVERERROR_MSG. In this scenario, this could be pretty useful, because the Authentication class can pass success/error information around and external classes can know what the text means, even if it’s changed in another translation. Another class could do something like the following to authenticate a user:


Authentication userauth = new Authentication( servername, user, pass );
var result = userauth.authenticate();
if ( result != Authentication.SUCCESS_MSG )
{
  displayError( result );
}

This is a really simplified example–and there is probably a better example that didn’t immediately come to mind–but you get the idea. Real constants are supported in Actionscript 3.0 using the const syntax, but if you’re working on an AS2 project, there’s still a solution for you that will give you the same effect.

What will the next generation of Make: look like? We’re inviting you to shape the future by investing in Make:. By becoming an investor, you help decide what’s next. The future of Make: is in your hands. Learn More.

Tagged
Discuss this article with the rest of the community on our Discord server!

ADVERTISEMENT

Escape to an island of imagination + innovation as Maker Faire Bay Area returns for its 16th iteration!

Prices Increase in....

Days
Hours
Minutes
Seconds
FEEDBACK