6 Replies Latest reply on Jan 29, 2009 1:24 PM by Newsgroup_User

    Make a method only except constants from a certain class?

    john isaacks Level 1
      Say I have a class called StoneProperties and in this class are 3 constants SIMULATED, GENUINE, PREMIUM which are all strings.

      Now I have a method that expects a constant from the StoneProperties class. right now can just except a string paramater and check if the string matches any of those 3 strings, but thats not very solid because it assumes StoneProperties will never have any new constants added to it. Is there a solid way to do this?

      Thanks!
        • 1. Re: Make a method only except constants from a certain class?
          Level 7

          "john isaacks" <webforumsuser@macromedia.com> wrote in message
          news:glstsh$p3v$1@forums.macromedia.com...
          > Say I have a class called StoneProperties and in this class are 3
          > constants
          > SIMULATED, GENUINE, PREMIUM which are all strings.
          >
          > Now I have a method that expects a constant from the StoneProperties
          > class.
          > right now can just except a string paramater and check if the string
          > matches
          > any of those 3 strings, but thats not very solid because it assumes
          > StoneProperties will never have any new constants added to it. Is there a
          > solid
          > way to do this?

          Could you be more specific? The whole point behind constants is that
          they're a known value. If you add a constant, you're almost guaranteed to
          have to go into the class and change the logic anyway. If we knew what your
          actual goal was, maybe we could give you better advice.


          • 2. Make a method only except constants from a certain class?
            levancho Level 3
            so you want Java ENUM in flex :) ?
            there is no enum feature in flex, you can use a class instead of String but that does not add much enforcement besides class type .but you can pass function pointer instead




            • 3. Re: Make a method only except constants from a certain class?
              john isaacks Level 1
              OK here is what I am trying to do:

              The stone composition is set via a URL fragment to enable deep linking. so when the url changes (among many other things) it checks what the fragment for stone composition is set to, it should always be one of the constants in the StoneProperties class, but since the user can simply type whatever they want into the URL, I need a way to verify that.
              • 4. Re: Make a method only except constants from a certain class?
                levancho Level 3

                I would create private array for example called :
                types:Array = ["SIMULATED", "GENUINE", "PREMIUM"]

                then the method isValid (type:String) : boolean) {
                return types.contains(type);
                }

                so if you want to add more types you just add item to array and isValid method does not need to change.






                • 5. Re: Make a method only except constants from a certain class?
                  john isaacks Level 1
                  Thanks that will work, I did not know there was a contains() method on arrays! Thats nice to know too!

                  • 6. Re: Make a method only except constants from a certain class?
                    Level 7

                    "john isaacks" <webforumsuser@macromedia.com> wrote in message
                    news:glt4a1$3kd$1@forums.macromedia.com...
                    > OK here is what I am trying to do:
                    >
                    > The stone composition is set via a URL fragment to enable deep linking. so
                    > when the url changes (among many other things) it checks what the fragment
                    > for
                    > stone composition is set to, it should always be one of the constants in
                    > the
                    > StoneProperties class, but since the user can simply type whatever they
                    > want
                    > into the URL, I need a way to verify that.

                    Doesn't deep linking imply that there is an actual structure, such as a View
                    Stack pane or a State, attached to the URL? If that's so, why not just
                    check against the structure that the deep linking points to? Or give them a
                    combobox based off of available structures.