3 Replies Latest reply on Sep 15, 2008 12:56 PM by Newsgroup_User

    Variables Again

    GeorgeWS Level 1
      I have been at this for weeks. I have this:

      public function checkOut(event:Event):void {
      var tempStr:String = "";
      for (var i:int = 0; i < cartAryCol.length; i++) {
      tempStr = tempStr + cartAryCol .PNUM5W + "\n" ;
      Alert.show("Are you sure?", tempStr);
      gotoURL();
      }
      }

      It works perfectly gathering the PNUM5W (item numbers) and showing me them in an Alert. I want to sent the items to another function. Like this:

      public function gotoURL() : void {

      var url:String = (" http://www.youngsmarket.com/list.cfm");
      var variables:URLVariables = new URLVariables();
      variables.itemid = tempStr;
      var request:URLRequest = new URLRequest(url);
      request.data = variables;
      try {
      navigateToURL(request, 'Main');
      }
      catch (e:Error) {
      // handle error here
      }
      }

      The URL ends up with a null --- http://www.youngsmarket.com?itemid=null

      I think this should be easy. Can anyone tell me what Im doing wrong, or has an example.

      Thanks
      George
        • 1. Re: Variables Again
          ntsiii Level 3
          It is a scope problem. "tmpStr" only exists in the function it is declared. You could declare it in instance (global) scope, but it would be better to just pass your variable value into the second function:
          public function checkOut(event:Event):void {
          ...
          gotoURL(tempStr);
          }
          ...
          public function gotoURL(tempStr:String) : void {
          ...
          variables.itemid = tempStr;
          ...
          }

          Tracy
          • 2. Re: Variables Again
            GeorgeWS Level 1
            Tracy,
            Your Incredable; you have saved me so many times. It is sinking in slowly.

            G.
            • 3. Re: Variables Again
              Level 7

              "GeorgeWS" <webforumsuser@macromedia.com> wrote in message
              news:gama3e$4me$1@forums.macromedia.com...
              >I have been at this for weeks. I have this:
              >
              > public function checkOut(event:Event):void {
              > var tempStr:String = "";
              > for (var i:int = 0; i < cartAryCol.length; i++) {
              > tempStr = tempStr + cartAryCol .PNUM5W + "\n" ;
              > Alert.show("Are you sure?", tempStr);
              > gotoURL();
              > }
              > }
              >
              > It works perfectly gathering the PNUM5W (item numbers) and showing me them
              > in
              > an Alert. I want to sent the items to another function. Like this:
              >
              > public function gotoURL() : void {
              >
              > var url:String = (" http://www.youngsmarket.com/list.cfm");
              > var variables:URLVariables = new URLVariables();
              > variables.itemid = tempStr;
              > var request:URLRequest = new URLRequest(url);
              > request.data = variables;
              > try {
              > navigateToURL(request, 'Main');
              > }
              > catch (e:Error) {
              > // handle error here
              > }
              > }
              >
              > The URL ends up with a null --- http://www.youngsmarket.com?itemid=null
              >
              > I think this should be easy. Can anyone tell me what Im doing wrong, or
              > has an
              > example.

              When you define a var inside a function, it ceases to exist as soon as you
              exit the function. I'm amazed you're not getting a warning when you try to
              use it inside a different function.

              Try passing it to the other function like so:

              public function gotoURL(tempStr:String):void{
              //stuff
              }

              then you'd call it like this:
              gotoURL(tempStr);


              HTH;

              Amy