2 Replies Latest reply on Aug 28, 2007 5:37 AM by MannenMytenLegenden

    Defining variables from a class gives errors

    MannenMytenLegenden
      Hi,

      I'm trying to create a questionnaire, that can hold an array of questions.
      The questions can be of different types, like single punch, multi punch, open text etc.

      Much of the code below is copied from examples I've found and I am definitely in the beginning
      of my learning process when it comes to Flex, but I think Flex can solve a problem I have at work.
      So, I'm trying to cut corners...

      I created this class called Questions, which contains all properties any question can have.
      When I'm trying to define a variable (myQuestion) based on that class, I the following error.
      (actually I get it on all properties...)

      Error:
      1120: Access of undefined property Qtype.
      ...

      This is the class definition:
      package Mycomponents
      {
      //import other classes needed
      //import Mycomponents.Codeframe

      public class Question {

      // Define properties and methods.
      // Define public vars.
      public var Qtype:String="";
      public var Condition:String="";
      public var Qnumber:String="";
      public var Qheading:String="";
      public var Pretext:String="";
      public var Qtext:String="";
      public var Posttext:String="";
      public var Codeframe:String;
      public var Reserved:String="";
      public var EditableFrom:String="";
      public var EditableTo:String="";
      public var Decimal:String="";
      public var SetQfactor:String="";
      public var Logic_if:String="";
      public var Logic_then:String="";

      // Public constructor.
      public function Question(
      Qtype:String,
      Condition:String,
      Qnumber:String,
      Qheading:String,
      Pretext:String,
      Qtext:String,
      Posttext:String,
      Codeframe:String,
      Reserved:String,
      EditableFrom:String,
      EditableTo:String,
      Decimal:String,
      SetQfactor:String,
      Logic_if:String,
      Logic_then:String )
      {
      this.Qtype=Qtype;
      this.Condition=Condition;
      this.Qnumber=Qnumber;
      this.Qheading=Qheading;
      this.Pretext=Pretext;
      this.Qtext=Qtext;
      this.Posttext=Posttext;
      this.Codeframe=Codeframe;
      this.Reserved=Reserved;
      this.EditableFrom=EditableFrom;
      this.EditableTo=EditableTo;
      this.Decimal=Decimal;
      this.SetQfactor=SetQfactor;
      this.Logic_if=Logic_if;
      this.Logic_then=Logic_then;
      }
      }
      }

      This is the part of the code that generates the error:
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
      <mx:Script>
      <![CDATA[
      import mx.states.State;
      import mx.controls.Alert;
      import mx.events.CloseEvent;
      import flash.events.KeyboardEvent;
      import Mycomponents.Codeframe;
      import Mycomponents.Question;

      public var myQuestion:Question = new Question(
      Qtype,
      Condition,
      Qnumber,
      Qheading,
      Pretext,
      Qtext,
      Posttext,
      Codeframe,
      Reserved,
      EditableFrom,
      EditableTo,
      Decimal,
      SetQfactor,
      Logic_if,
      Logic_then
      );
      ....more code...

      ...what am I missing in defining this variable?
      If you have a link to a good book on Flex...let me know.

      Regards
      /Acke
        • 1. Re: Defining variables from a class gives errors
          JKohn99 Level 1
          When you create your local variable myQuestion, where are the parameters (used in the constructor) declared and initialized?

          var _qType = "some value";
          var myQ:Question = new Question(_qType,"","" etc);

          Also good OO practice is to declare your variables as private and add getters and setters for the values. Sometimes you may want to manipulate the values when getting or setting.

          If you use the standard AS3 get and set then declare your vars with an _
          so as not to conflict with the getter and setter. For exampe:

          public class MyClass
          {
          private var _var1:String;
          public function get var1():String
          {
          return _var1;
          }
          public function set var1(v:String):void
          {
          _var1 = v;
          }
          }

          This may seem like extra work, but in a complex application with lots of classes
          encapsulating updating of members will save you lots of headaches.
          • 2. Defining variables from a class gives errors
            MannenMytenLegenden Level 1
            Thanks I got that part to work!

            How do I turn Question into an array?

            Regards
            /Acke