3 Replies Latest reply on Dec 18, 2006 2:46 PM by phi2265

    how can i store a variable to a file?

    phi2265 Level 1
      for some reason i can't find much on writing to files, is this not possible?

      and if it isnt possible, how can i keep a variable? i don't want the variable to reset to 0 every time someone loads a new instance of the page.

      also, for some reason.. it seems to me that if statements dont work as they should. the vote() function always executes the countVote() function even if choice doesn't = ans.
      anyone know what im doing wrong

      example..
      private function vote(choice):void {
      text1.text=choice
      if (choice=ans){
      countVote();
      }
      }

      private function countVote():void {
      total=total+1
      totalS=total.toString();
      text2.text=totalS
      }
      text2 displays the total count and every time i submit the vote, the count goes up no matter what i choose


      i also get a warning for all of the variables saying
      "1084: var 'total' will be scoped to the default namespace: sv1: internal. It will not be visible outside of this package." (sv1 is the name of the project)
      how to i make it visible outside of the package?
        • 1. Re: how can i store a variable to a file?
          ntsiii Level 3
          First, the famous "double equal" In AS (and javascript) the single "=" operator is an assignment, not a comparison. So when you do:
          choice=ans
          you are alwasys SETTING choice equal to ans. Instead, use the double equal operator:
          choice == ans
          This one STILL catches me sometimes.

          Next, on the save a variable question, you can persist data to a Local Shared Object. Check the docs, this is pretty easy, post back if you have difficulty.

          Third, ALL functions and instance-level (not declared in a function) variables should be explictily scoped. the main options are public, private, and there are a couple others.

          Tracy
          • 2. Re: how can i store a variable to a file?
            FlightGuy Level 1
            Interesting you got that problem with the == vs =. The compiler provides a warning if you do this with anything but an assignment to an explicitly declared Boolean variable or parameter. Hence in your example I would expect two warnings: one on the line of your function declaration about parameter 'choice' having no type declaration, and another warning on the if() line saying "Assignment in conditional. Did you mean == instead of =?". My experience says "never ignore a warning" - if you have a problem and there's a compiler warning, your first assumption should be that it may be related. Interestingly, in this single case, failing to declare a type on the Choice parameter actually saves you from the assignment error because it causes a warning - if you actually declare the type of the choice parameter (and perhaps you did and just didn't transcribe it in your post), it provides no warnings at all.

            That said - I have also been caught by it before. Though I'll say not nearly as often these days with smart compilers in AS and Java as the old days with C!

            Tim
            • 3. Re: how can i store a variable to a file?
              phi2265 Level 1
              ahh.. the == makes sense now. i must have had somethig wrong when i originally tried the == instead of =, but it works now. thanks.

              as for the shared object.. i'm looking into that now. this is all new to me so i'm trying to figure out how it is used.