Grizzly
Grizzly

Reputation: 5943

Access to new class object's properties

If I am creating a new class object and assigning its properties values like so:

foreach (var item in lstOfPersonnel)
{
    ClassNameVM personVM = new ClassNameVM ()
    {
        Name = item.FirstName + " " + item.LastName,
        TimeTotalPFID = lstSummaries.Where(x => x.PFID == item.ID).Sum(x => x.Time),
        TimeTotalPNFID = lstSummaries.Where(x => x.PNFID == item.ID).Sum(x => x.Time),
        TimeTotalVO = lstSummaries.Where(x => x.VO == item.ID).Sum(x => x.Time),
        TotalTime = // trying to do TimeTotalPFID + TimeTotalPNFID + TimeTotalVO.. but I can't access those properties.
    };

    newListPersonnel.Add(personVM);
}

How do I access the TimeTotalPFID, TimeTotalPNFID, TimeTotalVO properties within the object itself?

Upvotes: 0

Views: 65

Answers (3)

Chris
Chris

Reputation: 27599

In this case if you want the total time to just be the sum of other properties then don't work it out each time. This is error prone and doesn't respond correctly if the other properties change. Best in this case is to just have a property that only has a get which does the calculation for you...

public class ClassNameVM
{
    /*other properties as normal */
    public int TotalTime
    {
        get { return TimeTotalPFID + TimeTotalPNFID + TimeTotalVO; }
    }
}

Upvotes: 1

Rion Williams
Rion Williams

Reputation: 76547

You cannot access those properties as they technically don't exist within the scope of that anonymous object call.

Consider setting that property on the line below the declaration:

ClassNameVM personVM = new ClassNameVM ()
{
    Name = item.FirstName + " " + item.LastName,
    TimeTotalPFID = lstSummaries.Where(x => x.PFID == item.ID).Sum(x => x.Time),
    TimeTotalPNFID = lstSummaries.Where(x => x.PNFID == item.ID).Sum(x => x.Time),
    TimeTotalVO = lstSummaries.Where(x => x.VO == item.ID).Sum(x => x.Time),
};

// Set your properties here
personVM.TotalTime = personVm.TimeTotalPFID + personVM.TimeTotalPNFID + personVM.TimeTotalVO;

Consider a Constructor

Alternatively, you could define a constructor that would take in a parameter of your item type and use that:

public ClassNameVM(YourItemType item)
{
    Name = item.FirstName + " " + item.LastName;
    TimeTotalPFID = lstSummaries.Where(x => x.PFID == item.ID).Sum(x => x.Time);
    TimeTotalPNFID = lstSummaries.Where(x => x.PNFID == item.ID).Sum(x => x.Time);
    TimeTotalVO = lstSummaries.Where(x => x.VO == item.ID).Sum(x => x.Time);
    TotalTime = TimeTotalPFID + TimeTotalPNFID + TimeTotalVO;
}

Which would allow you to more easily use:

foreach (var item in lstOfPersonnel)
{
    newListPersonnel.Add(new ClassNameVM(item));
}

Upvotes: 6

Lyubomir Dimov
Lyubomir Dimov

Reputation: 141

Easiest solution would be:

ClassNameVM personVM = new ClassNameVM ()
        {
            Name = item.FirstName + " " + item.LastName,
            TimeTotalPFID = lstSummaries.Where(x => x.PFID == item.ID).Sum(x => x.Time),
            TimeTotalPNFID = lstSummaries.Where(x => x.PNFID == item.ID).Sum(x => x.Time),
            TimeTotalVO = lstSummaries.Where(x => x.VO == item.ID).Sum(x => x.Time),

        };
      personVM.TotalTime = personVM.TimeTotalPFID + personVM.TimeTotalPNFID + personVM.TimeTotalVO;

Upvotes: 3

Related Questions