flex/actionscript 分配失败?

发布于 2024-09-02 12:25:47 字数 1278 浏览 2 评论 0原文

我在我的动作脚本代码中看到一些奇怪的东西,

我有两个类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放低过去 2024-09-09 12:25:48

使用 as 关键字进行转换失败时会返回 null。在这种情况下,数组集合中的第一项可能不是您期望的 Bar 类型的对象;它可以是 Foo 或其他东西。您可以将子类对象强制转换为基类对象,但反之则不行。

使用括号语法进行转换 - 如果转换失败,它会抛出异常,因此您可以找出 arr.getItemAt(0) 的类型。

//Change 
var tempBar:Bar = arr.getItemAt(0) as Bar;

//to 
var tempBar:Bar = Bar(arr.getItemAt(0));

确保数组集合中的第一项确实是 Bar 实例(而不是 Foo 或其他东西)。

否则,您可以使用以下方式找到类型:

trace(flash.utils.getQualifiedClassName(arr.getItemAt(0)));

if(tempBar != null) {
  tempBar.someProp++;
  f = tempBar;
  // f is now null
}

顺便说一句,我相信发布的代码不是您运行的确切代码,因为 fnull, tempBarf 时,code> 应为 null。在这种情况下,当您检查 if 内的 null 时,不应执行 if 内的代码。即使它进入 if 块,它也会在您尝试递增 tempBar.someProp 的第一行抛出空指针错误 (#1009)

Casting using the as keyword returns null when it fails. In this case, the first item in the array collection may not be an object of type Bar as you expect; it could be a Foo 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).

//Change 
var tempBar:Bar = arr.getItemAt(0) as Bar;

//to 
var tempBar:Bar = Bar(arr.getItemAt(0));

to make sure that the first item in the array collection is indeed a Bar instance (and not Foo or something else).

Otherwise you can find the type using

trace(flash.utils.getQualifiedClassName(arr.getItemAt(0)));

if(tempBar != null) {
  tempBar.someProp++;
  f = tempBar;
  // f is now null
}

By the way, I believe the posted code is not the exact code you ran because for f to be null, tempBar should be null as you assigning it to f. In that case the code inside if should not be executed as you're checking for null inside if. Even if it enters the if block, it will throw a null pointer error (#1009) in the first line where you're trying to increment tempBar.someProp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文