ActionScript: Specification of the is operator - actionscript-3

I am having trouble googling for the expected behavior of the is operator in AS3. What I need to know is what should happen if the left side is null.
I know it is possible to test, and get empirical results, but I need to know what the specification says, and cannot find it anywhere.

You were not looking hard enough.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/operators.html#is
"Evaluates whether an object is compatible with a specific data type, class, or interface."
Because null is kinda SPECIAL CASE and "The Null data type is special in that it is not associated with a class" you will get false while using it with in operator.

The ActionScript 3 Language Specification says the following about the is operator:
The is operator checks to see if the value on the left is a member of
the type on the right. For user-defined types and most built-in types,
is returns true if the value is an instance of a class that is or
derives from the type on the right, otherwise it returns false.
Since null belongs to the special type Null which can't be extended and cannot be used on the right side of the is operator, the is operator will always return false.
| Value | String | Number | int | uint | Boolean | Object |
|-------|--------|--------|-------|-------|---------|--------|
| null | false | false | false | false | false | false |

You can find a bit more about how is operator works from this Adobe article:
It shows this example:
if (xParam is String) { /* do some String things */ }
else if (xParam is Number) { /* do some Number thing */ }
It shows that is operator is for type-checking. Consider var myNumber : dataType = new dataType. Now is the unknown dataType classed as Number?, int?, String? or Bitmap? This method allows you to create function code that anticipates what data format is input and can deal accordingly.
You dont use is to check for a null but alternatively you can use the == equality operator.
if (xParam == null) { /* do IS NULL things */ }
else { /* do some IS NOT NULL thing */ }

Related

Proper term for 'if (object)' as a non-null test?

In ActionScript 3, you can check if a value exists like this:
if (object) {
trace("This object is not null, undefined, or empty!")
}
I frequently use this as a shorthand for if (object != null)
Is there a proper term for evaluating objects for null in this fashion? I suppose it's a matter of the Boolean typecasting rules for the language but I'm not sure if there's a name for the resulting syntax.
If your object is always an actual object reference, e.g. a variable of any object type, then checking if (object) is a valid way to test for nulls. If it's a property of variant type, or a dynamic property that can potentially contain simple values (ints, strings etc) then the proper way to test for null will be explicit conversion, probably even with a strict type check if (object !== null).
Like most computer languages, Ecma-script supports Boolean data types;
values which can be set to true or false. In addition, everything in
JavaScript has an inherent Boolean value, generally known as either
truthy or falsy
list of falsy values (non truthy)
false
0 (zero)
"" (empty string)
null
undefined
NaN (a special Number value meaning Not-a-Number!)
regularly for checking if a variable is null
you may use : (a == null) this statement returns true if a is null, But also return true for all of the above list, because they'r all falsy values and (a == undefined) returns true, even a is not undefined but null or 0 or false.
so you should use Identity operator in this case. following evaluations just returns true when a is null
(typeof a === "null")
// or
(a === null)

Access VBA - Argument not optional - backcolor

I have a table where the user can select a colour scheme for their database
ID | Colour | SelectedScheme
---|------------|--------------
1 | 136,207,39 | Yes
Then on my form I have the following on the current events tab
Dim RGBcolor As Long
RBGcolor = DLookup("Colour", "SelectedScheme")
Me.boxheader.BackColor = RGB(RGBcolor)
but I'm getting the Argument not optional error. any idea?
Arguments of RGB function are numeric, not string, and there should be 3 of them:
RGB(136,207,39)
You are trying to pass one string argument.
You can use Split function for extracting color values from string:
Me.boxheader.BackColor = RGB(Split(RGBcolor,",")(0),Split(RGBcolor,",")(1),Split(RGBcolor,",")(2))
The Dlookup function accepts these arguments:
FieldName: a field, calculation, control on a form, or function that you wish to return;
TableName: the set of records. This can be a table or a query name;
Criteria (Optional): It is the WHERE clause to apply to the TableName.
And the RGB functions accepts these three:
red: the red component of the color;
green: the green component of the color;
blue: the blue component of the color.
I would suggest you to use three different columns for the component color in your table:
ID | ColourR | ColourG | ColourB | SelectedScheme
---|---------|---------|---------|---------------
1 | 136 |207 | 39 | Yes
Your event can be written as this:
Me.boxheader.BackColor = RGB(
DLookup("ColourR", "SchemaTableName", "SelectedScheme=TRUE"),
DLookup("ColourG", "SchemaTableName", "SelectedScheme=TRUE"),
DLookup("ColourB", "SchemaTableName", "SelectedScheme=TRUE")
)
this will select the color from the table SchemaTableName where the field SelectedScheme is set to true.
The RGB() function expects 3 numbers, but your code supplies a single string value. That is the cause of the "Argument not optional" compile error.
Construct a new string which includes the RGB() function plus your string of numbers. Submit that new string to Eval() ...
Me.boxheader.BackColor = Eval("RGB(" & RBGcolor & ")")

Return true when query gives 1

I want to save a true/false in my MySQL database. I'm saving 1/0 in an INT column to do this. When I select it, I get the 1 or 0, but I want it to return true/false to my PHP code, without having to rewrite the database.
Can I use another column type? Should I save it differently?
Update: My question is about not wanting to rewrite the returned value. I'm getting a lot of results from my database. Many of those are true/false, but some are 0s because the price is 0, so I don't want to universally rewrite all 1s and 0s. I also don't want to manually rewrite 10 columns.
To follow up my comment, here's a more detailed response which also covers the PHP side, although this probably belongs on StackOverflow.
I've always just used tinyint, although you can use bool/boolean which are synonyms for tinyint(1)
However as of MySQL 5.0.3 you can use the bit type:
As of MySQL 5.0.3, the BIT data type is used to store bit-field values. A type of BIT(M) enables storage of M-bit values. M can range from 1 to 64.
Next, assuming you have an active column, perhaps to store if a user is active, you could use PHP's automatic type conversion to handle this quite simply.
// Obviously you'd replace this with your database call
$results = [['active' => 1], ['active' => 0]];
foreach($results as $row) {
if ($row['active'] == true) {
echo "true\n";
}
else {
echo "false\n";
}
}
You don't strictly need to do anything.
PHP does not, and can not, use strongly typed variables. So, if you receive an (int) 1 from your query results, you can simply use this 1 as a boolean without rewriting or changing anything.
$intOne = (int) 1; //explicitly treat the variable as an integer
var_dump((bool) $intOne); //treat the variable as a boolean
When used in any boolean context, like if ($variable)... then any of these types will be considered to be false by PHP:
the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
... And, most importantly;
Every other value is considered TRUE (including any resource).
Source: PHP Manual > Booleans (english)
So while you can change the storage type of your column in mysql, this won't really change the way PHP handles the variable retrieved from your results at all.
Historically, I've always used a column of type TINYINT(1) to store boolean values in mysql, and as Tom Green points out, recent mysql versions provide a new BIT type, which might be appropriate. To the best of my knowledge, mysql does not currently have an actual boolean data type.
You could just as easily use a column of type VARCHAR(1), though, because PHP can and will use any value as a boolean, thanks to the glorious, majestic, and sometimes maddening, PHP Type Juggling.
If you're trying to use the values you're retrieving for boolean logic, just use the values you receive from mysql like booleans and it will work:
if ($valueFromResults) {
//The value was something like true
} else {
//The value was something like false
}
If you're trying to actually echo out the words "true" and "false", then you're probably best served by explicitly echoing the words out yourself, like this;
if ($valueFromResults) {
echo "true";
} else {
echo "false";
}
or, in my preferred shorthand;
echo ($valueFromResults) ? "true" : "false" ;
Update You mentioned in a comment that you want to pass the values through json_encode() and use them in javascript.
JavaScript treats any real value, like int 1, as true and any empty value, like int 0, or an empty string, as false. So if your json_encode() output gets used in actual JavaScript, the int values will still work as boolean values. So the integer values from your database should still work as they are.
Just check that your integer results are encoded as integers by PHP and not as strings - they should be encoded correctly by default - because "0" == true in javascript, but 0 == false.
For a boolean value (true/false), you should use the mySql type bit or tinyint(1).
$boolean = $mysql_data ? true : false;

EBean mapping Booleans defaulting to false instead of null

I've been using Scala + EBean and I have a problem;
I have a model that looks a bit like this;
case class SomeModel(name: String) extends Model {
var someBool: Boolean = _
}
The problem is that even though the default value of someBool is null in the schema, EBean fills it up with 0 (it maps it to a TINYINT in mySQL), I should be able to save a null in the fields as well.
(ideally I'd like to keep track of whether or not the field has been set to a value in the model), wherein a null for a field would mean the field hasn't been filled in yet.
What would be the best way to solve this?
A possible solution to this is to simply replace Boolean with java.lang.Boolean, so;
case class SomeModel(name: String) extends Model {
var someBool: java.lang.Boolean = _
}

Handling tinyInt booleans with MVC

I have a mySQL table setup with a column with a datatype of TinyInt(1). This represents a boolean.
0 = false; 1 = true.
I'm also setting up a website to show this information to the company using asp.net and the MVC design. Right now, the table shows the column as:
"Are Records Online"| [next column]
------------------------------------
1 | junk
1 | junk
0 | junk
However, I want the 1's and 0's to display as 'true/false' or 'yes/no'. Basically just something more user-friendly.
How should I do this? With a script on the Viewpage itself or with a foreach loop in the controller such as:
foreach(ListViewModel i in DefaultList)
{
if(i.RecordsOnline == true)
{ //set to 'yes'
}
else if(i.RecordsOnline = false)
{ //set to 'no'
}
}
Just use a simple ternary to print the appropriate bit of text in your view:
#(i.RecordsOnline ? "Yes" : "No")
You can use type ENUM('yes', 'no') instead of TINYINT. The same one byte per value, but more user-friendly.
You could create a DisplayTemplate based on the bool type since you appear to be converting the tinyint to bool in your model.
-- YesNo.cshtml
#model bool
#(Model ? "Yes" : "No")
Place that folder inside of ~/Views/Shared/DisplayTemplates
You can use it like so: (assumes RecordsOnline is part of your model and is a bool)
#Html.DisplayFor(m => m.RecordsOnline, "YesNo")