Tom
Tom

Reputation: 8127

Howto allow any data type to be returned by a function in actionscript 3?

I have a static Settings class where my application can retrieve settings from. The problem is that some of these settings are strings, while others are ints or numbers. Example:

package
{
    public final class Settings
    {
        public static function retrieve(msg:String)
        {
            switch (msg)
            {
                case "register_link":
                    return "http://test.com/client/register.php";
                    break;
                            case "time_limit":
                                   return 50;
                                   break;
            }
        }
    }
}

Now, in the first case it should send a string and in the second a uint. However, how do I set this in the function declarement? Instead of eg. function retrieve(msg:String):String or ...:uint? If I don't set any data type, I get a warning.

Upvotes: 1

Views: 176

Answers (2)

Tyler Egeto
Tyler Egeto

Reputation: 5495

HanClinto has answered your question, but I would like to also just make a note of another possible solution that keeps the return types, typed. I also find it to be a cleaner solution.

Rather than a static retrieve function, you could just use static consts, such as:

package
{
    public final class Settings
    {
        public static const REGISTER_LINK:String = "my link";
        public static const TIME_LIMIT:uint= 50;            
    }
}

And so forth. It's personal preference, but I thought I would throw it out there.

Upvotes: 3

HanClinto
HanClinto

Reputation: 9461

Use *

public static function retrieve(msg:String):*
{
  if (msg == "age") {
    return 23;
  } else {
    return "hi!";
  }
}

Upvotes: 2

Related Questions