7 Replies Latest reply on Mar 16, 2011 1:53 PM by chris.schruder

    Declaring a variable of a class

    chris.schruder

      Hi,

       

      I'm creating a stack to save math operations that have occurred so that I can use an undo button to remove those operations.  I followed this implementation:

       

      http://deviltechie.wordpress.com/2010/10/03/create-a-stack-queue-data-structure-in-flex-ac tionscript/#comment-71

       

      I created two actionscript class files Node.as and Stack.as like what was done in the link but I do not know how to create a variable of that class in the Script area of my .mxml file.  My code looks like this:

       

          <fx:Script>
              <![CDATA[
                  import dataStore.Node;
                  import Stack;

       

                  var storage:dataStore.Stack;

       

              ]]>
          </fx:Script>

       

      I was trying to create a variable called storage of type Stack but I get an error saying "1046: Type was not found or was not a compile-time constant: Stack."

       

      Any information on how to correctly create a variable of a stack class in the Script of my main MXML file would be very helpful.

       

      Thanks

        • 1. Re: Declaring a variable of a class
          _spoboyle Level 4

          That is the correct way to decalre a class variable however it is good practice to also include an access modifier (public, private, protected)

           

          The error you are getting is indicating that it cannot find the class Stack. if you have used the Stack class from the link then the import should be

               import dataStore.Stack

           

          if you delete the import Stack line and just type Sta  within the script block and press ctr+space then Flash Builder will offer auto complete options if it can find a class beginning with Sta and automatically add the import statement for you

          1 person found this helpful
          • 2. Re: Declaring a variable of a class
            chris.schruder Level 1

            Thanks for the quick reply _spoboyle.  In the case for adding access modifiers, I should allow for public access of this variable since the program needs to access the Stack.as file right?

             

            I changed my code to look like this:

             

                <fx:Script>
                    <![CDATA[
                        import dataStore.Node;
                        import dataStore.Stack;

             

                        var storage:Stack;

                    ]]>
                </fx:Script>

             

            It doesn't say that it cannot find the class Stack now but I get this error in the Stack.as file:

             

                 A file found in a source-path must have the same package structure '', as the definition's package, 'dataStore'.    Stack.as   

             

            Now what the previous link said to do was to create two .as files with package names dataStore.  I'm kind of confused on this error.

            • 3. Re: Declaring a variable of a class
              pauland Level 4

              If Stack is in package "dataStore", it should declare it self as belonging to package dataStore AND the source should sit in a directory called dataStore.

              • 4. Re: Declaring a variable of a class
                _spoboyle Level 4

                public allows the variable to be access by other classes via an instance of the Stack class as well as within the Stack class itself

                private allows the variable to only be accessed within the Stack class

                protected allows the variable to be access within the Stack class and any subclasses of Stack

                 

                 

                without knowing the structure of your project and what exactly you are trying to achieve I cannot tell what access storage variable requires (at i guess i would suggest provate would be sufficient)

                 

                as for the error we are making progress it can now find the Stack class but

                 

                the Stack class has been declared in a package called dataStore (see first line of Stack.as) this means that class must be in a folder called dataStore and the dataStore folder must be in the root of the src folder.

                 

                in flash builder right click on your src folder goto New then Package. give it the name dataStore and then move Stack.as into this folder

                1 person found this helpful
                • 5. Re: Declaring a variable of a class
                  chris.schruder Level 1

                  Thanks spoboyle and pauland,

                   

                  By adding a new package called dataStore I was able to create a variable of Stack so you two will get the answer and helpful answer to my question. 


                  Currently I am getting a error when I try to use the push and pop functions from the Stack.as file.  The error is this:

                   

                  Error #1009: Cannot access a property or method of a null object reference.

                   

                  I am not sure if I am calling the functions incorrectly in the code or if the Stack.as code I borrowed from the link above is incorrect. What I have is two buttons, "1" and "undo".  If the "1" is pressed the total value gets subtracted by 1 and number 1 gets pushed into the stack.  If "undo" is pressed then the 1 gets popped from the stack and added to the total.  Here is an implementation of my code trying to use the push and pop functions:

                   


                  <fx:Script>
                          <![CDATA[
                              import dataStore.Node;
                              import dataStore.Stack;

                   

                              public var storage:Stack;

                   

                              protected function one_clickHandler(event:MouseEvent):void
                              {
                                 
                                  var tot:Number =int(total.text);
                                 
                                 
                                  var amount:Number=int(1);
                                  tot=tot-1;
                                  total.text = String(tot);
                                  storage.push(amount);
                                     
                              }

                   

                              protected function undo_clickHandler(event:MouseEvent):void
                              {
                                  var num:Number =int(storage.pop());
                                  var tot:Number =int(total.text);
                                  tot=tot+(num);
                                  total.text = String(tot);
                                 
                              }

                   

                          ]]>
                      </fx:Script>

                   

                  I receive this error everytime I press the "1" button or the "undo" button when I am running the application.  Any info would be greatly appreciated.

                  • 6. Re: Declaring a variable of a class
                    pauland Level 4

                    You need to initialize storage - it's not automatically created by declaring it.

                     

                    storage is a reference to an object - until you create the object it will be null.

                     

                    storage = new Stack();

                     

                    (

                    You could just do

                     

                    public var storage:Stack = new Stack();

                     

                    though I usually use a constructor.

                    )

                     

                    Paul

                    • 7. Re: Declaring a variable of a class
                      chris.schruder Level 1

                      Thanks a lot Paul.  Undo button works like a wonder.  Appreciate the help.

                       

                      Chris