I have this function:
private function returnFees(row:Object,name:String):int
{
var fee:int;
for each(var obj:Object in row)
{
if(obj==name)
{
//fee=obj as int;
}
}
return fee;
}
I want to compare the name of properties to the 'name'. But in this code the 'obj' is giving me the values of properties not the name.
Any ideas? thank you
It's not a for each you have to use but a simple for. for each will give you the values and for the property name :
private function returnFees(row:Object,name:String):int {
var fee:int;
for (var rowName:String in row) {
if(rowName == name) {
//fee=obj as int;
}
}
return fee;
}
Related
How can I get the data from this function using AS3? Or at least how to properly convert it to AS3?
Original AS-2 Class A was :
mc.btn.onRollOut = function(data)
{
dispatchEvent({type: "itemRollOut", target: _parent.data});
itemRollOut(data);
}
public function itemRollOut(data)
{
}
So I tried to convert it like this below : new AS-3 Class A
mc.btn.addEventListener(MouseEvent.MOUSE_OUT, btnonRollOut);
function btn.btnonRollOut(evt:MouseEvent)
{
itemRollOut(data);
}
public function itemRollOut(data)
{
// here returns the data in the other class -- > mc.itemRollOut = function(data:Object)
}
This Class B shown below calls the data from the itemRollOut in Class A:
mc.itemRollOut = function(data:Object)
{
trace(data + " : " + data.data);
}
What I'm trying to achieve :
When I itemRollOut my button, I want to pass the data from the result that Class A gives into Class B.
Thanks In Advance.
Are you asking you to define a function such as;
private function getSquare(nbr:Number):Number
{
return nbr * nbr;
}
which when called like;
var nbrOutvalue:Number = getSquare(20);
will return 400?
If so the answer is the :Number at the end of the function definition line, which is the data type of the value to return.
I cannot seem to access an array of custom objects (that is a column in a Parse table) after querying for it and receiving the results.
I have a simple custom class call "TextEntry" that contains 2 strings.
public class TextEntry
{
public string key;
public string text;
public TextEntry() { }
}
I have a ParseObject subclass called "LocalePO", which has an IList member in addition to other native types.
[ParseClassName("LocalePO")]
public class LocalePO : ParseObject
{
[ParseFieldName("version")]
public int version
{
get { return GetProperty<int>("version"); }
set { SetProperty<int>(value, "version"); }
}
[ParseFieldName("code")]
public string code
{
get { return GetProperty<string>("code"); }
set { SetProperty<string>(value, "code"); }
}
[ParseFieldName("name")]
public string name
{
get { return GetProperty<string>("name"); }
set { SetProperty<string>(value, "name"); }
}
[ParseFieldName("keypair")]
public IList<object> keypair
{
get { return GetProperty<IList<object>>("keypair"); }
set { SetProperty<IList<object>>(value, "keypair"); }
}
public LocalePO() { }
}
I can query to Parse and successfully return a LocalePO object, but I cannot access the specific "TextEntry" members of the "keypair" List afterwards.
var cloudQuery = new ParseQuery<LocalePO>();
var queryTask = cloudQuery.FirstAsync();
// wait for query to return
while (!queryTask.IsCompleted) yield return null;
LocalePO locale = queryTask.Result;
int CloudVersion = locale.version; // this works
List<TextEntry> list = new List<TextEntry>();
list = locale.keypair.Cast<TextEntry>.ToList(); // this doesn't work
foreach (var item in locale.keypair)
{
var entry = item as TextEntry; // this does not work
TextEntry entry = (TextEntry)item; // this doesn't work either
// this is my current solution which works but seems terrible
string json = JsonConvert.SerializeObject(item);
TextEntry entry = JsonConvert.DeserializeObject<TextEntry>(json);
list.Add(entry);
}
I feel like I am overlooking something very simple here, but I just want to convert the data I pull from Parse to local objects so I can use the data throughout the app logic.
It seems to me that Parse prefers the IList of type "object"vs an IList of type "TextEntry" type for the ParseFieldName. For example, Parse always returns null for the field if I have the following:
[ParseFieldName("keypair")]
public IList<TextEntry> keypair
{
get { return GetProperty<IList<TextEntry>>("keypair"); }
set { SetProperty<IList<TextEntry>>(value, "keypair"); }
}
Perhaps I should derive TextEntry from ParseObject too? I'm so confused.
Any help would be appreciated.
Thanks!
Try this:
var cloudQuery = new ParseQuery<LocalePO>();
cloudQuery .Include("keypair");
var queryTask = cloudQuery.FirstAsync();
TextEntry will need to derive from parseObject and you will need to register it as a subclass.
Here is an example of a query I am using which has 2 levels of nested iList's of parseObject subclasses
ParseObject.RegisterSubclass<ProgramDataParse>();
ParseObject.RegisterSubclass<WorkoutDataParse>();
ParseObject.RegisterSubclass<ExerciseDataParse>();
var programQuery = new ParseQuery<ProgramDataParse>()
.OrderByDescending("createdAt").Limit(2)
.Include("workouts")
.Include("workouts.exercises");
I am trying to create custom iterator element. The class itself is below.
public class ArrayBasedIterator implements IteratorInterface
{
// Holds Array of elements which are iterated
private var _container:Array;
// Holds current index
private var _index:uint = 0;
public function ArrayBasedIterator(data:Array)
{
if (!data)
throw new Error("Cannot create iterator for null data");
_container = data;
}
/**
* Returns next node if it exists, null otherwise*/
public function next():TreeNode
{
if (_index > _container.length )
{
return null;
}
else {
_index += 1;
}
//TODO: implement function
return _container[_index];
}
/**
* Returns prev node if it exists, null otherwise*/
public function prev():TreeNode
{
if (_index < 0)
{
return null
}
else {
_index -= 1;
}
//TODO: implement function
return _container[_index];
}
public function len():int
{
return _container.length;
}
}
The question is. In case I have such iterator over some elements, would it be possible to use for .... in structure? What do I need to change to use it? (Except using just array instead of iterator of course)
If you want to use for ... in, you'll need to subclass Proxy and override the appropriate methods:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Proxy.html
I have this ObjectType class which is a class to help me do something like this:
object.type = ObjectType.TWO
//ObjectType.as
package
{
public class ObjectType
{
public static var ONE:String = "one";
public static var TWO:String = "two";
public static var THREE:String = "three";
public function ObjectType()
{
}
}
}
Let's suppose I'm creating a new class and I need a property named type. In that property set function I want to make sure that it's value is one of the ObjectType variables. How can I achieve this?
public function set type(value:String):void
{
for (var o:Object in ObjectType) {
if (value == o)
this._type = value;
} else {
//error
}
}
}
Not performance aware but without modifying anything you can use describeType function to check the static field and get the value back:
function valueInClass(clazz:Class, value:*):Boolean {
return describeType(clazz).variable.(clazz[#name.toString()] == value).length() != 0
}
public function set type(value:String):void
{
if (valueInClass(ObjectType, value)) {
this._type = value;
} else {
//error
}
}
I suppose the second code example you presented doesn't work...
I think it is because you're using the for in loop a little bit wrong.
for (var blah:String in somewhere){
// blah represents a KEY of the somewhere object
// to get the value of this key, use:
var theValue = somewhere[blah];
}
It's the for each loop that loops through the values. But for now I'll use the for in.
Also, it's not in ObjectType, but rather in the class' prototype, that is in ObjectType.prototype.
So, to fix this:
for (var o:* in ObjectType.prototype) {
if (value == ObjectType.prototype[o])
this._type = value;
} else {
//error
}
}
You can solve this using reflection.
A similar question was asked just a few days ago, you should be able to use the same solution, found here.
It should be noted that while the the accepted answer is right, it's also really slow. Not something that you want to do a lot. There are three simpler solutions.
One: Check the value itself:
public function set type(value:String):void
{
if( value != ObjectType.ONE && value != ObjectType.TWO && value != ObjectType.THREE )
return;
}
Obviously, the more constants you have the check the harder this becomes.
Two: Use ints as your constants
Change your ObjectType class to use ints:
public class ObjectType
{
public static var NONE:int = 0;
public static var ONE:int = 1;
public static var TWO:int = 2;
public static var THREE:int = 3;
public static var TOTAL:int = 4;
}
Notice the NONE and TOTAL in there? This makes it easy to check if your value is in the right range:
public function set type(value:int):void
{
if( value <= ObjectType.NONE || value >= ObjectType.TOTAL )
return;
}
You can add more values as needed and you just need to update TOTAL and it'll still work. This needs each value to be in order though.
Three: Use Enums
While Flash has no in-build class for enums, there's a lot of solutions available. Check our the Enum class from Scott Bilas: http://scottbilas.com/blog/ultimate-as3-fake-enums/
Using this as your base class your ObjectType class becomes:
public final class ObjectType extends Enum
{
{ initEnum( ObjectType ); } // static ctor
public static const ONE:ObjectType = new ObjectType;
public static const TWO:ObjectType = new ObjectType;
public static const THREE:ObjectType = new ObjectType;
}
And your check now becomes:
public function set type(value:ObjectType):void
{
...
}
Here, your setter now becomes type safe and will throw errors if anything other than an ObjectType is used.
It turns out that if using an ENUM type of check you should check for the constants property, not variables as showin in the example here:
ActionScript - Determine If Value is Class Constant
I am making a javaFX class and I need one of the variables to be initialized in order for it to work (in my program there's no default value I can use). This is the best I've come up with, but I'd like something that wont compile unless you initialize the variable.
Example Class:
Public class Class1{
public-init var var1:String;
postinit{
if(var1 == null){
println("You need to initialize var1");
}
}
I'd call it like this:
var object1 = Class1{var1:"input"};
How can I prevent it from compiling if I do this?
var object1 = Class1{};
Unfortunately, I think you have the best solution for forcing initialization. Only other thing you can do is set a default value:
public var var1: String = "BOGUS";
You can use this:
public class Class1 {
public var var1: String = "" on replace{
if (var1 == null) {
var1 = "";
}
};
}
var object1 = Class1{};
println(object1.var1);
object1.var1="HOLA :)";
println(object1.var1);
Output:
Mundo
HOLA
:)
Or maybe:
public class Class1 {
public-init var var1: String;
init {
if (var1 == null) { //or var1. length() == 0 ) {
println("You need to initialize var1");
Stage {
title: "Ups!!!"
onClose: function() {
}
scene: Scene {
content: [
Label {
text: "You need to initialize var1"
}
]
}
}
}
}
}