How to change the value of an object property in actionscript3? - actionscript-3

I'll start by showing my code.
var modules:Object = new Object();
modules = DPServices.getModules.lastResult;
for each (var item:Object in modules){
if(item.menu == 0){
// Don't know what to do here!!
}
}
modulesDG.dataProvider = modules;
By the way this is ActionScript 3.
What I am trying to do is change the value from a 0 to 'No' or a 1 to 'Yes'. I have tried modules.menu = 'no', modules.item.menu = 'no', and modules.#menu = 'no'. So how do I change this value?

Is modules an object of objects? If not, this should be fine:
item.menu = 'no';

You want to change menu to 1 if it's 0?
Isn't it enough to just do:
if(item.menu == 0){
item.menu = 1;
}
Though I'm not exactly sure what you're asking for.

Related

How to get value of selected index in combo Box when user select the value from it in flex

Hi I have created a comboBox dynamically and Since I'm new to Flex I have no idea how to get the selected value from combo box when user select a value from combobox dropdown
Below is my code
var comboBox:ComboBox = new ComboBox();
comboBox.dataProvider = field.getValues();
comboBox.width = "50";
comboBox.prompt = "Test";
comboBox.selectedIndex = -1;
Could someone help me out in order to identify how I'll be able to get value of a selected index when user will select the value from dropdown of combo box ?
Even a sample example will help me !!
Thanks in Advance.....!!
You can use comboBox.selectedItem.
Remember to check for null as selectedItem will return null if it is not set.
comboBox.addEventListener(ListEvent.CHANGE, comboBox_change, false, 0, true); //weak listener
private function comboBox_change(event:Event):void {
var comboBox:ComboBox = event.target as ComboBox
var item:MyClass = comboBox.selectedItem as MyClass
if(item) {
//do what you need to do
}
}
You can do like following way:
var comboBox:ComboBox = new ComboBox();
comboBox.dataProvider = field.getValues();
comboBox.width = 50;
comboBox.prompt = "Test";
comboBox.selectedIndex = -1;
comboBox.addEventListener(ListEvent.CHANGE, onChange);
panel.addChild(comboBox);
private function onChange(event:Event):void
{
trace(event.currentTarget.selectedItem); //Here you get the selected item.
}
Hope it helps.

Getting NaN(Not a Number) in Adobe Flash

I wanted to make a little gas calculator in Flash with AS but i am getting the error "NaN" in my textfield even BEFORE i enter anything inside the textfield. Any ideas where the problem is? Many thanks in advance. Here is my actionscript code:
km_txt.restrict = ".0-9";
liter_txt.restrict = ".0-9";
priceliter_txt.restrict = ".0-9";
stage.addEventListener(Event.ENTER_FRAME, calculate);
function calculate(param1:Event)
{
if (liter_txt.text != "" && km_txt.text != "")
{
usage_txt.text = String(100 * Number(liter_txt.text) / Number(km_txt.text));
}
if (liter_txt.text != "" && km_txt.text != "" && priceliter_txt.text != "")
{
cost_txt.text = String(Number(liter_txt.text) / Number(km_txt.text) * Number(priceliter_txt.text));
}
if (liter_txt.text != "" && priceliter_txt.text != "")
{
total_txt.text = String(Number(liter_txt.text) * Number(priceliter_txt.text));
}
}
You are casting to Number a few times from TextField objects but those at that point don't contain anything so the cast resolve to NaN:
String(100 * Number(liter_txt.text) / Number(km_txt.text));
Now trying to add/multiply/divide Number and NaN together still resolve to NaN.
You need to check for value first and maybe set to 0 if you get NaN, store in variables to make things easier:
var value:Number = Number(liter_txt.text);
if(isNaN(value))
{
//this is not a number so substitute with 0?
value = 0;
}
Be sure about initial text values of your input TextFields since you are checking value of "". At the top of your block, write;
km_txt.text = "";
liter_txt.text = "";
priceliter_txt.text = "";
And it will be a better code if you listen to TextEvent.TEXT_INPUT events on TextFields and make calculations there.

if statement and visible statement dont work but the things inside it work? ACTIONSCRIPT 3

Yeah so here's my code
if(defaultmeter.visible = true)
{
meter1.visible = true;
meter1.x = 124.10;
meter.y = 63.10;
jizz.visible = false;
}
Thing is the things inside the { }
work but the the if statement doesnt apply
Like when defaultmeter is not visible, the stuff inside the {} still applies :C help please
If you do mc.visible = true so you assign the true value to mc.visible so you make mc visible which normally always true.
To compare in this level, we use == (equal) operator to check if two values are equal or the != (not equal, different ) to check if two values are not equal.
So in your case you can do :
if(defaultmeter.visible == true){
// instructions here
}
Or
if(defaultmeter.visible != false){
// instructions here
}
Or simply
if(defaultmeter.visible){
// instructions here
}
Well of course, you need to cover the other case as well, like this:
if(defaultmeter.visible == true)
{
meter1.visible = true;
meter1.x = 124.10;
meter.y = 63.10;
jizz.visible = false;
}else{
meter1.visible = false;
jizz.visible = true; //btw, jizz, seriously?
...
}
use like this if (defaultmeter.visible == true){}

ActionScript 3 - how to set empty display object variable?

What I initially wanted to do is this:
if (myTargetName == 'a') {
var myOtherTargetName:String = "b";
var myOtherTarget:DisplayObject = getChildByName(myOtherTargetName);
}
if (myTargetName == 'c') {
var myOtherTargetName:String = "d";
var myOtherTarget:DisplayObject = getChildByName(myOtherTargetName);
}
as you can see, I get a 'Duplicate variable definition' error since im declaring the variables twice. My solution I thought would be to just declare those variables outside the if statements, and change the variable inside the statement, like so:
var myOtherTargetName:String = ""
var myOtherTarget:DisplayObject = None;
if (myTargetName == 'a') {
myOtherTargetName = "b";
myOtherTarget = getChildByName(myOtherTargetName);
}
if (myTargetName == 'c') {
myOtherTargetName = "d";
myOtherTarget = getChildByName(myOtherTargetName);
}
This gives an error saying "access of undefined property None". Now, I don't want to set
myOtherTarget:DisplayObject
to an actual existing display object which is on the stage just yet, I want to set it to an object on the stage inside the if statements. Is there a way to set
myOtherTarget:DisplayObject
to nothing?
It is easy, copy+paste it:
var myOtherTarget:DisplayObject = null;
or simply:
var myOtherTarget:DisplayObject;

Set initial combobox selection DevExpress Razor

I simply want to have the "Area" value (which is one of the items) be selected when the view appears. This is what I have:
#Html.DevExpress( ).ComboBox( settings =>
{
settings.Name = "cmbFieldLevel";
settings.CallbackRouteValues = new { Controller = "Equipment", Action = "FieldLevelPartial" };
settings.Properties.ValueType = typeof( string );
settings.Properties.TextField = "AreaName";
settings.Properties.ValueField = "AreaID";
settings.Properties.EnableClientSideAPI = true;
settings.ClientSideEvents.DataBound = "function( s, e ){ cmbFieldLevel.SelectedItem = "Area"; } ";
}).BindList(FieldLevel.GetAreaFilters()).GetHtml()
Any clues?
2 ways I can think of.
If you change your ComboBox to ComboBox for you can specify the model's value
like so
#Html.DevExpress().ComboBoxFor(x => x.ParamOrderNo, settings =>
{
settings.Properties.ValueField = "OrderNo";
settings.Width = 200;
settings.Properties.TextField = "Name";
}).BindList(CeduleProductionMVC.ViewModels.ViewModelCeduleGlobale.GetCommandes()).GetHtml()
Also, you can set the SelectedIndex, if your combolist content is fixed, the index might always be the same everytime all the time. Also, If you list is not fixed, you might be able to make a method to retrieve the index and set in after that.