Just trying to understand some code - function

and good evening,
i have just come across the following code:
void showButtons(boolean mode)
{
int state = (mode) ? View.VISIBLE : View.GONE;
l.btn1.setVisibility(state);
l.btn2.setVisibility(state);
l.btn3.setVisibility(state);
l.btn4.setVisibility(state);
}
and i not sure how this works exactly, will be grateful for any explanation, thanks in advance. (specially the int state line).

The function sets the Visibility of btn1, btn2, btn3, and btn4 (probably buttons) to VISIBLE or GONE depending on input.
showButtons(true); sets Visibility VISIBLE.
showButtons(false); sets Visibility GONE.
The top line, int state = (mode) ? View.VISIBLE : View.GONE; sets the value state to View.VISIBLE if mode is true, or View.GONE if mode is false.
It uses an operator called the "conditional operator", which is a little like an if...then...else. It works like:
variable = test_value ? answer_if_test_true : answer_if_test_false;

Related

Serial monitor problem instantly finishing

int pinRed=2;
int pinBlue=4;
int pinWhite=6;
String Color;
String question="Which LED would you like to light up Red,Blue or White?";
int delayTime=1000;
void setup() {
Serial.begin(9600);
pinMode(pinRed,OUTPUT);
pinMode(pinBlue,OUTPUT);
pinMode(pinWhite,OUTPUT);
}
void loop() {
Serial.print(question);
while(Serial.available()==0){
}
Color=Serial.readString();
if(Color=="Red" || Color=="red"){
digitalWrite(pinRed,HIGH);
digitalWrite(pinBlue,LOW);
digitalWrite(pinWhite,LOW);
delay(delayTime);
}
if(Color=="Blue" || Color=="blue"){
digitalWrite(pinBlue,HIGH);
digitalWrite(pinRed,LOW);
digitalWrite(pinWhite,LOW);
delay(delayTime);
}
if(Color=="White" || Color=="white"){
digitalWrite(pinWhite,HIGH);
digitalWrite(pinBlue,LOW);
digitalWrite(pinRed,LOW);
delay(delayTime);
}
delay(delayTime);
}
This is my code, but is not working, when I enter a color the serial monitor ask the question again instantly and not led is turning on, I checked the pins and the LEDs work completely fine but I think I have a problem with my serial monitor cause I am having problem with every project using the serial monitor despite even copying codes from the internet, so anyone have any idea what can be the problem(I am on version Arduino 1.8.18)
I do not know for sure as I am not familiar with the particular language/library that you are using. However, the symptoms you describe indicate that your input string does not match any of your targets ('red', 'RED') etc.
I would re-read the description of the Serial.available() function. Is it returning non-zero as soon as the first character is available? If so then your input string from
Color=Serial.readString() will always be aone-character string, eg 'r'.
To test this you could try matching against 'r', 'g', 'b' as strings.....

getItemProperty returns not null - Vaadin bug?

I need help, maybe I am blind. Here is a fragment of my code:
System.out.println("itemPropertyIDS="+item.getItemPropertyIds().toString());
System.out.println("argname="+argName);
Property<?> p = item.getItemProperty(argName);
if (p != null) {
System.out.println("p="+p.toString());
return p.getValue();
}
// Continue ...
It returns a currious null value instead of continue, even if the propertyId doesn't exists.
This is written on my console:
itemPropertyIDS=[iconName, iconResource, nodeType, nodeValue, nodeName, handler, nodeData]
argname=Lab
p=com.vaadin.data.util.IndexedContainer$IndexedContainerProperty#12967
The first row shows list of property names.
I expected getTtemProperty must return null, but not.
The item comes from IndexedContainer.
Can you help me? Any idea? Thanky You.
I tested your code and indeed - property p is not null even though property doesn't exist in IndexedContainer. Reading the comments of Vaadin ticket pasted by kris54321, it makes sense not fixing the bug as some applications may rely on that feature. Fixing the bug may break those apps.
Possible workarounds for this problem:
Check directly the propertyId-collection if property exists in the container:
if(item.getItemPropertyIds().contains(argName) {
Property<?> p = item.getItemProperty(argName);
System.out.println("p="+p.toString());
return p.getValue()
}
Change the logic to check property value
if(item.getItemProperty(argname).getValue() != null) }
//Do things
{
I was forced to make workaround using "item.getItemPropertyIds().contains(argName)". For me, the priority is to check whether the item contains the value (null possible) or not. If yes then I use the value (can be null) else another activity is done.
If they dont want to fix the bug due to application using the bug, then the documentation should be fixed, else all novices in Vaadin (as I am) will be confused.
The implementation in IndexedContainer$IndexedContainer is wrong:
#Override
public Property getItemProperty(Object id) {
return new IndexedContainerProperty(itemId, id);
}
This can never return null as the documentation says.

Coding a triple-state Boolean in AS3

I know that a Boolean can have only 2 values, false and true. And I can code a Class that has a property that can have 3 states. But how can I code it in such a way so that, when setting or getting its state, I can refer to the object itself as opposed its property? eg:
var triBoolean:TriBoolean = 0;
// later...
triBoolean = 1;
The reason I want to do this is so I can have a value that toggles through three states and I'm assuming it would be quicker to refer directly to the object - but perhaps that's not right.
Or is it only primitive types that can be used in this way?
If you use an Object type you can do it, having: (null, true or false) tri-state, you will lose type safety, but still valid:
var triBoolean:Object = null;
// Then you can assign your triple states...
triBoolean = false; //valid
triBoolean = true; //valid
triBoolean = null; //valid

AS3 Boolean seemingly not working

Ok, so this is obviously going to be something that I stupidly overlooked in my code, but I am having problems with a boolean check in as3. In the below if statement I set a boolean, I can confirm that the boolean is set in this if switch as I have run a trace to check that:
if(switchA && switchB){
if(Side == "LEFT"){
localAttachCoords.x = (-Parent.collision.SideLength - entity.collision.SideLength)/2
localAttachCoords.y = Parent.collision.SideLength/2 - (((TargNode-1)*8) + entity.collision.SideLength/2)
}
if(Side == "RIGHT"){
localAttachCoords.x = (Parent.collision.SideLength + entity.collision.SideLength)/2
localAttachCoords.y = -(Parent.collision.SideLength/2 - (((TargNode-1)*8) + entity.collision.SideLength/2))
}
if(Side == "UP"){
localAttachCoords.y = (Parent.collision.SideLength + entity.collision.SideLength)/2
localAttachCoords.x = -(Parent.collision.SideLength/2 - (((TargNode-1)*8) + entity.collision.SideLength/2))
}
if(Side == "DOWN"){
localAttachCoords.y = (-Parent.collision.SideLength - entity.collision.SideLength)/2
localAttachCoords.x = Parent.collision.SideLength/2 - (((TargNode-1)*8) + entity.collision.SideLength/2)
}
entity.attached = true
entity.Parent = Parent
}
This would all be well and good, but for the fact that in a function from another class, executed every frame, claims that the boolean was set to false, I confirmed this with another trace function.
This is the function, taken from the class whose instance is referred to as entity in the above switch:
public function update(){
if (physics) physics.update()
if (node && physics){
trace(attached)
if(attached){
physics.nodeUpdate()
}
}
}
This function claims in the trace that attached == false despite it being set true earlier with no other reference to the attached variable. Any help would be appreciated!
Pathing
There are some un-addressed variables in your issue, foremost being the pathing you're taking to check your variable. This is relevant because of namespaces/scope affect what each piece of code has access to.
If your functions and variables shared the same space (i.e., global/document/timeline), then any reference the the same named variable will always return the same value, unless (as LoremIpsum noted) it's being shadowed by a local variable by the same name.
Obviously, this is not the case since you're using public function which is a class-only declaration. If the boolean you're looking for is on the timeline, and the class wants to read that variable, you need to have a valid path to it. Instantiated classes that are DisplayObjects and have been added to the DisplayList have both parent and stage properties which you can use to access the timeline global namespace (thereby providing access to your boolean).
However, if the class is not a DisplayObject (e.g., it does not extend a Sprite, Shape, or MovieClip), then access to the timeline has to be provided manually, either by setting a property on the class, or passing an argument to a method on the class.
Further complicating the matter is if the Boolean exists in another class object (either instantiated or static), you'd then need a way to get between them. A case of A sees B, C sees B, but neither A or C see eachother.
Values
A boolean is always going to be false, even if the assigned value was null, so if your class is trying to reference a variable that it can't see, that value will always be false. For example...
var foo:Boolean = this["fiddlesticks"];
trace("foo = " + foo); // traces: "foo = false"
There is no property this.fiddlesticks, so while the resolved value is null, foo becomes false. Consider using hasOwnProperty(), which indicates whether an object has a specified property defined, and is a method available to all objects.
Switch
You don't have to manually create your own switch using if then else if, AS3 has its own switch statement.
switch (Side) {
case "LEFT":
// Do stuff for left
break;
case "RIGHT":
// Do stuff for right
break;
case "UP":
// Throw your hands up
break;
case "DOWN":
// Get down and boogie!
break;
}
I hope that all helps. I'd like to say exactly what's going on with the access to your Boolean, but there simply isn't enough information to say.
Cheers!

Limit Flex Spark DataGrid ItemRenderer data to column

I've got a DataGrid in my mobile application (I know, I know but there's currently not other solution for this), that numeric values. Depending on the value of a cell, the text gets colored. My big issue is that this doesn't work very for more than about 5 rows of data. I reckon the issue is that the set text function (in which I format the color) can't keep up with the amount of cell changes and formats the text based on the last updated cell of the row.
I thought about a column item renderer so that the renderer only gets the value for that column, and not the whole data of the row.
Is something like that possible?
Just for reference, this is my current item renderer (again, this works fine for few rows, for 5+ of fast changing data, this doesn't work any more, cells get formatted even though their data hasn't changed).
public class ColorGridItemRenderer extends DefaultGridItemRenderer
{
public function ColorGridItemRenderer() {
super();
}
override public function set text(value:String):void {
if (!value)
value = "";
if(Number(value) > Number(text) && text!="")
setStyle("color", 0x40c040);
else if(Number(value) < Number(text) && text!="")
setStyle("color", 0xf05050);
else
setStyle("color", 0xc1c1c1);
}
super.text = value;
}
}
Edit:
I just double-checked and have to revert my previous statement. A single row works fine, the second row already messes up the color formatting though. It seems as if the datagrid somehow throws together the values.
Doing the color formatting in the set data method shows the exact same effect;
override public function set data(value:Object):void {
if(value && data) {
label = value[column.dataField];
if(value[column.dataField] > _oldVal && _oldVal != 0)
setStyle("color", 0x40c040);
else if(value[column.dataField] < _oldVal)
setStyle("color", 0xf05050);
else
setStyle("color", 0xc0c0c0);
_oldVal = value[column.dataField];
}
super.data = value;
}
Edit2:
My assumption about the grid messing up the ItemRenderer's data seems to be correct.
A simple trace in the set data method (trace("old: " + _oldVal + "new : " + value[column.dataField]);) revealed that somehow, values of the next grid row (in case there are 2) get used as well as _oldVal gets the old value of the next row.
Just for reference:
Calling super on the overridden function sets the value to the superclass, thus mixing the values of the rows. Leaving it from the function solves the problem.