tags:

views:

43

answers:

3

So here's what I mean. Let's say I have a class ErrorMessages which holds all my error messages as static constants. So I could access them like ErrorMessages.PASSWORD_INVALID or ErrorMessage.PASSWORD_TOO_SHORT. I want to know if it is possible to have separate classes that hold subset of these constants and access them like ErrorMessages.PASSWORD.INVALID or ErrorMessages.PASSWORD.TOO_SHORT, etc. This way I can more structured static structure and makes it much easier to use autocomplete. I tried few different ways and couldn't figure out if this was possible..

+2  A: 

Declare them as const Objects in the static class - you won't get them in auto complete though.

public class ErrorMessages
{
  public static const PASSWORD:Object = {
     INVALID:"invalid password",
     TOO_SHORT:"minimum 6 chars required",
     TOO_LONG:"100 chars: r u sure?"
  };
  public static const FILE:Object = {
     NOT_FOUND:"No such file",
     READ_ONLY:"it is readonly",
     SOMETHING_ELSE:"something else"
  };
}

trace(ErrorMessages.PASSWORD.INVALID);

If auto complete is important, create a dedicated com.domain.errors package and declare different classes for different categories of errors (like PASSWORD, FILE etc) within that package. Now declare public static constants inside those classes as appropriate.

Amarghosh
I see... I do want to get autocomplete.. one of the my initial reason for wanting this was so that I can do ErrorMess<tab> . Pass<tab> . Inv<tab> to get ErrorMessage.PASSWORD.INVALID. Currently if I want to get ErrorMessage.PASSWORD_INVALID, I would have to type PASSWORD_INV to get the correct result. and I'd like to keep it in a single file. I just need few handful of constants and think having separate file for them might be overkill..Thanks for the suggestion though
lordofthefobs
A: 

or if you want to keep a single class, you can define classes, inside your Error class. You might would like to have those text coming from properties file. So, you can make use of resourceManager instance and get the text from specific resource bundle.

-- http://riageeks.com

Chetan Sachdev
I would like to keep a single file. I tried defining classes inside my ErrorMessage class, but I just couldn't find a way to expose this class via static variable of ErrorMessage class so that I can access the static consts.I tried something like thispackage com.domain.data.type { public class ErrorMessages{ public static const PASSWORD.... }}class PasswordError { public static const INVALID:String = "invalid password";}I just couldn't figure out how I can access static consts of PasswordError through the ErrorMessage.PASSWORD
lordofthefobs
A: 

Here's what I end up doing

package com.domain.data.type {
  public class ErrorMessages {
    public static function get PASSWORD:PasswordErrorMessages { return new PasswordErrorMessages(); }
    }
}
class PasswordErrorMessages {
  public function get INVALID():String  { return "invalid password"; }
}

This way I can get the behavior I wanted: ErrorMessages.PASSWORD.INVALID with autocomplete. It's not as clean as I'd like it to be.. but I guess this will do.

lordofthefobs
I hope you realize that you're creating a new object every time you read the error message.
Amarghosh
yeah I changed it to a static variable.
lordofthefobs