Goldentp
Goldentp

Reputation: 197

Error using Container Class

The Class that is using the container is throwing an error on AmountList and PGRow that says AmountList is inaccessible due to its protection level

The Code

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;

}

I tried making the class public, do I have to do something else to make the class accessible to getAjaxPGs??

Upvotes: 0

Views: 90

Answers (1)

Oded
Oded

Reputation: 499132

By default, class members are private - the are only accessible to the class itself.

You need to declare them as public if you want to access them from outside the class (or internal if the code that requires access only ever exists in the same assembly).

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

Note: the above (public access to fields) is considered bad practice - you should be using properties for this.

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

Upvotes: 1

Related Questions