5 Replies Latest reply on Jun 13, 2006 4:07 AM by blemmo

    Variable Problem

    sbryner Level 1
      Can someone please help me figure out what I'm doing wrong?
      I get "undefined" where the vFirst and vLast are in the text field.

      thanks,

      sky
        • 1. Re: Variable Problem
          blemmo Level 1
          Well, the variables aren't defined when you use them in the string for the 'storyA' var. Besides, there's no 'Var' class, so the lines
          var vFirst = new Var;
          var vLast:String = new Var;
          don't make sense. I guess you meant something like
          var vFirst:String = new String();
          var vLast:String = new String();

          The vars get defined in the onRelease event, so you should assign the storyA var also there, after the other vars are defined. The variables get evaluated at the moment of using, so when you use them before that, the result will be 'undefined', as you saw.

          hth,
          blemmo
          • 2. Re: Variable Problem
            sbryner Level 1
            Thank you, I got it to work.

            what is the difference between?

            var vFirst;
            and
            var vFirst:String = new String();
            • 3. Re: Variable Problem
              blemmo Level 1
              The 2nd var gets typed to String, that means it can only hold string values, no numbers or other types. The 1st var is a var that can hold any type of data.
              It's not really a big difference, but it may help avoid mistakes, because the compiler will show an error message when a typed variable is assigned wrong:
              var str:String = 5;
              will lead to an error of "type mismatch", but
              var str;
              str = "foo";
              str = 5;
              str = new Array();
              will be ok, because it's not typed, but it may lead to confusion about what that var is supposed to hold. I prefer typing to make it clear what kind of data this var is supposed to store.

              cheers,
              blemmo
              • 4. Re: Variable Problem
                sbryner Level 1
                thanks again,

                So, there is three ways to set a var? right

                var myVar; // blank variable has no specific attribute tied to it

                var myVar:String = new String(); // blank variable only contains a string
                or other specified data type

                var myVar="Hello" //variable with content hard coded inside.

                my next question is in a function say like this:

                var myVar="hello";

                myBtn_mc.onrelease = function () {
                myVar ="Tom";
                };

                does "Tom" replace "hello"?

                P.S. thanks blemmo for all the help. I'd be lost if it wasn't for the responses. I'm just learning this and some stuff in the documentation seems somewhat vague or twisted around. I wish they would just have a page of text commented out with what every line and symbol means breaking down everything from the function ( )'s to :Strings

                • 5. Re: Variable Problem
                  blemmo Level 1
                  Well the 3rd way to set a var is basically the same as the 1st, so I'd say there are 2 ways to create vars: typed and untyped.
                  The function example would replace "hello" with "Tom" (if you used the on Release event ;)), that's what functions are supposed to do: changing vars (besides some other things, probably). You just have to be careful that you're changing the right var, e.g.

                  var myVar="hello";
                  myBtn_mc.onRelease = function () {
                  this.myVar ="Tom";
                  };

                  wouldn't change the var containing "hello", but instead, it would create a var with value "Tom" inside the myBtn_mc instance, because 'this' inside events refers to the object that fired that event. So in this case the 2nd myVar is another var than the 1st myVar.
                  Hope that didn't confuse you... the code you posted was fine, I just thought I'd mention this.

                  greets,
                  blemmo