PDA

View Full Version : package, class


MadamZuZu
05-11-2007, 08:58 PM
hi :)

i keep getting this error:


1119: Access of possibly undefined property categories through a reference with static type Class. AOHotSpot.mxml AOHotSpot line 35 May 11, 2007 3:55:13 PM 221

i'v never tried classes in Flex before, but basically what i want to try and do is define categories in a class, and have the maina pplication populate a combo box based on it.

here is my main application code:


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
backgroundGradientColors="[#92abd1, #206d93]"
xmlns:local="*" >

<mx:Script>
<![CDATA[
import mx.controls.Label;
import flash.net.navigateToURL;


]]>
</mx:Script>

<mx:Panel width="70%" height="70%" layout="absolute" horizontalCenter="0" verticalCenter="-7.5">
<mx:HBox width="80%" height="33" horizontalAlign="center" verticalAlign="middle" top="0" horizontalCenter="-0.5">
<mx:Label text="AO Hotspot Category:" fontWeight="bold"/>
<mx:ComboBox></mx:ComboBox>
<mx:ComboBox
id="employeeDepartment"
dataProvider="{AOCat.categories}"
selectedIndex="{AOCat.category}"
change="{AOCat.category=employeeDepartment.selectedIndex;}"
/>


</mx:HBox>
<mx:List width="80%" height="227" horizontalCenter="0" y="41"></mx:List>
</mx:Panel>

</mx:Application>


and my package looks like this:

package
{
public class AOCat
{
private var _category:uint = 0;


[Bindable]
public var modelValid:Boolean = false;

[Bindable]
public var categories:Array;
public function AOCat()
{
categories =
[
"test", "test2", "test3"
];
}



[Bindable]
public function get category():uint
{
return _category;
}

public function set category(category:uint):void
{
_category = category;
}


public function get categoryName():String
{
return categories[_category];
}


}
}



do i need some sort of a include or something?
please ehlp :(

hangalot
05-11-2007, 10:21 PM
you need to create an instance of the class and use that. those methods are not declared as static methods. if you don't know what that means i recomend reading a book about OO programming.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
backgroundGradientColors="[#92abd1, #206d93]"
xmlns:local="*" >

<mx:Script>
<![CDATA[
import mx.controls.Label;
import flash.net.navigateToURL;
private var cats:AOCat = new AOCat();

]]>
</mx:Script>

<mx:Panel width="70%" height="70%" layout="absolute" horizontalCenter="0" verticalCenter="-7.5">
<mx:HBox width="80%" height="33" horizontalAlign="center" verticalAlign="middle" top="0" horizontalCenter="-0.5">
<mx:Label text="AO Hotspot Category:" fontWeight="bold"/>
<mx:ComboBox></mx:ComboBox>
<mx:ComboBox
id="employeeDepartment"
dataProvider="{cats.categories}"
selectedIndex="{cats.category}"
change="{cats.category=employeeDepartment.selectedIndex;}"
/>


</mx:HBox>
<mx:List width="80%" height="227" horizontalCenter="0" y="41"></mx:List>
</mx:Panel>

</mx:Application>

dr_zeus
05-11-2007, 11:38 PM
You need to understand that packages and classes are different things. Your second code example, AOCat, is a class. Packages are ways to organize common classes together. For instance, Flex's Label component is a class in the package mx.controls. HBox is a class in the package mx.containers.

Like hangalot mentioned, you actually need to create an instance of the class if you want to use it:

var instance:AOCat = new AOCat();
instance.category = 0;

You are using AOCat as if its properties were static, which they are not.