views:

153

answers:

2

Hi All,

I write a actionscript fileIncludeMyFile.as :

package CustomComponent
{
    public class IncludeMyFile extends Object
    {

        public function computeSum(a:int, a:int):Number
        {
            return a+b;
        }
    }
}

When i add package, i am getting Error: Package cannot be nested

And My Mxml file:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 <mx:Script source="CustomComponent/IncludeMyFile.as"/>  
 <mx:TextInput id="tinput1" width="40" height="40" textAlign="right" 
      x="100" y="100"/>
 <mx:TextInput id="tinput2" width="40" height="40" textAlign="right" 
      x="100" y="100"/>
 <mx:TextArea id="toutput" width="60" height="70" textAlign="right" 
      x="100" y="100"/>

 <mx:Button id="myButton" label="chckSum" 
     click="toutput.text(String(computeSum(Number(tinput1.text),
       Number(tinput2.text)));" x="130" y="140"/>

 <mx:Label text="+" width="40" fontWeight="bold" fontSize="20" x="130" 
      y="140"/>

</mx:Application>

Actually here, i include the Actionscript file into MXML.

Please let me know where i did wrong.

Thanks, Ravi

+1  A: 
<mx:Button id="myButton" label="chckSum" click="toutput.text(String(computeSum(Number(tinput1.text), Number(tinput2.text)));" x="130" y="140"/>

means you're using a plain mxml Button component.

Your function should be static as if doesn't seem you need to create an instance to compute a sum, and you have "a" and "b" as parameters, not a and a, Also, I hope you only need ints, otherwise if somewhere else you're expecting Number values(floating point values), you will them rounded.

public static function computeSum(a:int, b:int):Number{
   return a+b;
}

Are you sure addition is all you need to do ?

It might have been easier to have something like:

<mx:Button id="myButton" label="chckSum" click="toutput.text(String(Number(tinput1.text)+ Number(tinput2.text));" x="130" y="140"/>

This is just at a glance. it's all a bit dirty and I have no idea what you're actually trying to do.

George Profenza
A: 

This is what is causing the error:

<mx:Script source="CustomComponent/IncludeMyFile.as"/>

The compiler includes the file and interprets it as:

<mx:Script>
  <![CDATA[
  package CustomComponent
  {
    public class IncludeMyFile extends Object
    {
        public function computeSum(a:int, a:int):Number
        {
            return a+b;
        }
    }
  }
  ]]>
</mx:Script>

The <mx:Script> tag is expected to directly declare variables and methods. When the flex compiles an mxml file, it generates an AS file with its own package and class declarations. The name of a class declared by an mxml file is the name of that mxml file itself. When your include file contains its own package and class declarations, the compiler interprets it as nested package/class declarations and hence the error.

You cannot declare classes in the script included through script tags - you have to declare them separately. The script tag's source attribute is a way to write the corresponding actionscript in a separate file (which personally I don't prefer to do) and not to include other classes into your app. Use import statements within the script tag to do so.

To fix the error, change the AS file contents to:

public function computeSum(a:int, a:int):Number
{
    return a+b;
}

Now the method computeSum is an instance method of the class declared by your mxml file.

Amarghosh