1 Reply Latest reply on Sep 2, 2008 4:11 AM by Ventis85

    Property Problem

    BudHud
      Can you tell me why I am getting an error message in my Nproj.mxml file? This is an mxml/class project. I get the same error #1120: Access of undefined property myProduct. I get it seven times from these seven lines. What is wrong with myProduct?

      myProduct.setQuantity(3);
      myProduct.setPrice(2.99);
      myProduct.setDesc("Teddy Bear");

      trace(myProduct.getDesc());
      trace(myProduct.getPrice());
      trace(myProduct.getQuantity());
      trace(myProduct.getTotal());

      The class is listed in the file folder: Nproj/Product.as.

      This is my file, Nproj.mxml.

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">

      <mx:Script>
      <![CDATA[

      import Product;
      var myProduct = new Product();
      myProduct.setQuantity(3);
      myProduct.setPrice(2.99);
      myProduct.setDesc("Teddy Bear");

      trace(myProduct.getDesc());
      trace(myProduct.getPrice());
      trace(myProduct.getQuantity());
      trace(myProduct.getTotal());

      ]]>
      </mx:Script>
      </mx:Application>

      This is my actionscript class, Product.as.

      package
      {
      class Product {

      //Instance Variables
      private var price:Number;
      private var desc:String;
      private var quantity:Number;

      function Product() {
      }

      function getPrice():Number {
      return price;
      }

      function setPrice(myVar:Number):void {
      price = myVar;
      }

      function getQuantity():Number {
      return quantity;
      }

      function setQuantity(quantity:Number):void {
      this.quantity = quantity;
      }

      function getDesc():String {
      return desc;
      }

      function setDesc(myVar:String):void {
      desc = myVar;
      }

      function getTotal():Number {
      return price*quantity;
      }
      }
      }

      Thanks,

      BudHud
        • 1. Property Problem
          Ventis85 Level 1
          Probably because you haven't set any access levels for your class or your setters and getters.

          it should be:

          public class Product

          public function getPrice():Number

          public function setPrice(myVar:Number):void

          and so on.

          Also, although it's not required, try to do this for your variables and functions in your application file. Like so:

          private var myProduct = new Product();