flex/actionscript 分配失败?
我在我的动作脚本代码中看到一些奇怪的东西,
我有两个类 foo 和 bar,bar 扩展了 foo。在模型类中,我有一个 foo 成员变量,我将一个 bar 对象分配给 foo 变量。但赋值后 foo 变量为空。
[Bindable] public var f:foo;
public function someFunc(arr:ArrayCollection):void {
if(arr.length > 0) {
var tempBar:bar = arr.getItemAt(0) as bar;
if(tempBar != null) {
tempBar.someProp++;
f = tempBar;
// f is now null
}
}
}
关于我可能做错了什么有什么想法吗?
编辑 这是确切的代码:
[Bindable] public var selectedCustomerJob:IDSCustomer;
public function selectedJobByIdCallback(evt:Event):void
{
var temp:IDSDTOArrayCollection = evt.currentTarget as IDSDTOArrayCollection;
if(null != temp && temp.length > 0)
{
selectedCustomerJob = IDSJob(temp.getItemAt(0));;
trace(" selectedCustomerJob: " + flash.utils.getQualifiedClassName(selectedCustomerJob));
trace(" jobToSelect type: " + flash.utils.getQualifiedClassName(temp.getItemAt(0)));
trace("jobToSelect super class: " + flash.utils.getQualifiedSuperclassName(temp.getItemAt(0)));
}
}
这是跟踪输出:
selectedCustomerJob: null
jobToSelect type: com.intuit.sb.cdm.v2::IDSJob
jobToSelect super class: com.intuit.sb.cdm.v2::IDSCustomer
I'm seeing something weird in my actionscript code
I have two classes foo and bar, bar extends foo. In a model class I have a foo member variable, I assign an bar object to the foo variable. But after the assignment the foo variable is null.
[Bindable] public var f:foo;
public function someFunc(arr:ArrayCollection):void {
if(arr.length > 0) {
var tempBar:bar = arr.getItemAt(0) as bar;
if(tempBar != null) {
tempBar.someProp++;
f = tempBar;
// f is now null
}
}
}
Any ideas on what I could be doing wrong?
EDIT
Here is the exact code:
[Bindable] public var selectedCustomerJob:IDSCustomer;
public function selectedJobByIdCallback(evt:Event):void
{
var temp:IDSDTOArrayCollection = evt.currentTarget as IDSDTOArrayCollection;
if(null != temp && temp.length > 0)
{
selectedCustomerJob = IDSJob(temp.getItemAt(0));;
trace(" selectedCustomerJob: " + flash.utils.getQualifiedClassName(selectedCustomerJob));
trace(" jobToSelect type: " + flash.utils.getQualifiedClassName(temp.getItemAt(0)));
trace("jobToSelect super class: " + flash.utils.getQualifiedSuperclassName(temp.getItemAt(0)));
}
}
this is the trace output:
selectedCustomerJob: null
jobToSelect type: com.intuit.sb.cdm.v2::IDSJob
jobToSelect super class: com.intuit.sb.cdm.v2::IDSCustomer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
as
关键字进行转换失败时会返回null
。在这种情况下,数组集合中的第一项可能不是您期望的Bar
类型的对象;它可以是Foo
或其他东西。您可以将子类对象强制转换为基类对象,但反之则不行。使用括号语法进行转换 - 如果转换失败,它会抛出异常,因此您可以找出
arr.getItemAt(0)
的类型。确保数组集合中的第一项确实是
Bar
实例(而不是Foo
或其他东西)。否则,您可以使用以下方式找到类型:
顺便说一句,我相信发布的代码不是您运行的确切代码,因为
f
为null
,tempBarf
时,code> 应为null
。在这种情况下,当您检查if
内的null
时,不应执行if
内的代码。即使它进入if
块,它也会在您尝试递增tempBar.someProp
的第一行抛出空指针错误 (#1009)Casting using the
as
keyword returnsnull
when it fails. In this case, the first item in the array collection may not be an object of typeBar
as you expect; it could be aFoo
or something else. You can cast subclass object to base-class but not the other way.Use the parenthesis syntax for casting - it'll throw an exception if cast fails and thus you can figure out the type of
arr.getItemAt(0)
.to make sure that the first item in the array collection is indeed a
Bar
instance (and notFoo
or something else).Otherwise you can find the type using
By the way, I believe the posted code is not the exact code you ran because for
f
to benull
,tempBar
should benull
as you assigning it tof
. In that case the code insideif
should not be executed as you're checking fornull
insideif
. Even if it enters theif
block, it will throw a null pointer error (#1009) in the first line where you're trying to incrementtempBar.someProp