6 Replies Latest reply on Jul 26, 2008 12:30 PM by kglad

    Initialising a Method

    namaqua Level 1
      I have been using a method (derived from this forum) that worked fine in AS1:

      calling code:
      prefix = prefix.searchReplaceChars("[", "");
      or
      infotext1 = "<b>"+SimuArray[i+7].searchReplaceChars('$$$', '<br>')+"</b>";
      Method:

      string.prototype.searchReplaceChars = function(find, replace) {
      newString = this;
      if (newString.indexOf(find, 0)>=0) {
      FirstChar = 0;
      Start = -1;
      while (FirstChar>=0) {
      FirstChar = newString.indexOf(find, Start);
      if (FirstChar>=0) {
      if (find.length>=newString.length) {
      LastChar = find.length;
      } else {
      LastChar = FirstChar+find.length;
      }
      newString = newstring.slice(0, FirstChar) add replace add newstring.slice(LastChar, newString.length);
      Start = FirstChar+replace.length;
      }
      }
      }
      return newString;
      };


      Compiler error:

      **Error** Scene=Scene 1, layer=AS Layer, frame=1:Line 3921: There is no method with the name 'searchReplaceChars'.
      tempvar2 = tempvar.searchReplaceChars('%26', '&');

      Can you advise me how I need to initialise searchReplaceChars?

      Thanks
        • 1. Re: Initialising a Method
          kglad Adobe Community Professional & MVP
          1. that should be String, not string.

          2. you should not use that method until it's defined. ie, the prototype code needs to execute before your calling code.

          3. prototype is poorly coded.

          4. try:

          • 2. Re: Initialising a Method
            namaqua Level 1
            WOW! That's an elegant alternative!

            I'm still uncertain about:
            "2. you should not use that method until it's defined. ie, the prototype code needs to execute before your calling code."

            Do I just need to make sute it is processed by the compiler before it's called?

            Thanks
            • 3. Re: Initialising a Method
              namaqua Level 1
              Sorry, I still haven't got it right.

              I put the code:
              String.prototype.searchReplaceChars = function(find, replace) {
              return this.split(find).join(replace);
              }

              ..upstream of the call, and get the following compiler error:

              There is no method with the name 'searchReplaceChars'.
              prefix = prefix.searchReplaceChars("[", "");

              Can you suggest what I'm still doing wrong?
              • 4. Re: Initialising a Method
                kglad Adobe Community Professional & MVP
                for what fp are you publishing?
                • 5. Initialising a Method
                  namaqua Level 1
                  Player 7. In the mean time, by trial and error I found that I needed to match the souce and target string types. Since the original source was an array I needed to do the followng to make it work:


                  .. and then it worked. If I used strings as opposed to arrays, it showed the error message that I reported earlier.

                  Does that make sense?
                  • 6. Re: Initialising a Method
                    kglad Adobe Community Professional & MVP
                    that makes sense if you strictly type your strings. but it wouldn't make sense for you to that AND use a prototype function to extend a static class like the string class.

                    you really should not use prototype functions anyway. continue to type your variables and use: