繁体   English   中英

访问Class属性的Class属性

[英]Access classes property of Class property

码:

 public class ClassA
 {
      public decimal foo
 }

 public class ClassB
 {
   public List<ClassA> Alist 
 }

在ClassB内,我像这样向Alist填充值。

      foreach (junk in trash)
        {              
            Alist.add(new ClassA()
            {                   
                foo = junk.Bar
            });     
          // if (Alist.foo == whatever)
          // but  no access to Alist.foo here 
        }

在for each循环中,我试图对foo运行if语句。 所以像if (Alist.foo == whatever)这样的事情我无法做到,它不能让我访问Alist.foo 任何想法如何做到这一点。

问题是您没有对刚添加的对象的引用。 有很多方法可以做到这一点。 我建议您只需在Add外部构建对象。

var newA = new ClassA()
{                   
    foo = junk.Bar
};

// now you can perform the if statement
if (newA.foo == someValue)
{
    // do something
}

Alist.add(newA);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM