使用Container Class时出错

时间:2012-08-27 20:00:38

标签: c# class

使用容器的类在AmountList和PGRow上抛出一个错误AmountList is inaccessible due to its protection level

守则

public virtual ActionResult getAjaxPGs(string SP = null, string PG = null)
{

    if (SP != null)
    {

        Container container = new Container();
        var PGList = from x in db.PG_mapping
                     where x.PG_SUB_PROGRAM == SP 
                     select x;

        container.PARow = PGList.Select(x => new { x.PG }).Distinct().ToArray();

       container.AmountList = from x in db.Spend_Web
                        where x.PG == PG
                        group x by new { x.ACCOUNTING_PERIOD, x.PG, x.Amount } into pggroup
                        select new { accounting_period = pggroup.Key.ACCOUNTING_PERIOD, amount = pggroup.Sum(x => x.Amount) };

      return Json(container, JsonRequestBehavior.AllowGet);
    }

    return View();
}

public class Container
{
    Array PARow;
    Array AmountList;

}

我尝试将该类公开,我是否必须做其他事情才能让getAjaxPGs访问该类?

1 个答案:

答案 0 :(得分:1)

默认情况下,class members are private - 只能由类本身访问。

如果要从类外部访问它们,则需要将它们声明为public(如果只需要访问的代码存在于同一个程序集中,则需要internal。)

public class Container
{
    public Array PARow;
    public Array AmountList;
}

注意:以上(对字段的公共访问)被视为不良做法 - 您应该使用此属性。

public class Container
{
    public Array PARow {get; set;}
    public Array AmountList {get; set;}
}