Marc Rasmussen
Marc Rasmussen

Reputation: 20555

C# accessing subclass method by casting

I have the following abstract class:

abstract class ContactQueue
{

    public abstract DateTime period { 
        get; set; }
    public abstract String type { get; set; }
    public abstract String toString();
}

Now one of the sub classes of this class is the following:

class GeneralPercentageQueue : ContactQueue
{
    public GeneralPercentageQueue(DateTime period)
    {
        this.period = period;
    }
    public int phone_answer_total {get; set;}
    public int phone_answer_percentage_8025 { get; set; }
    public int web_answer_percentage_8030 { get; set; }
    public int web_answer_percentage_total { get; set; }
    public int mail_answer_percentage { get; set; }
    public override DateTime period { get; set; }
    public override string type { get; set; }
    public override string toString()
    {
        return period.ToString();
    }
}

Now since i have several subclass of the abstract class i have created a list that can contain them all i want to loop through that list and access one of the specefic fields to do this i have attempted the following:

foreach(ContactQueue cq in p.GetGeneralEmailPercentageData(start,end))
{
    foreach (ContactQueue contactqueue in finalDataList)
    {
        if (cq.period == contactqueue.period)
        {
           (GeneralPercentageQueue)contactqueue.mail_answer_percentage = (GeneralPercentageQueue)cq.mail_answer_percentage;
        }
    }
}

However im getting an error that there is no such field in the object ContactQueue

So how do i access it?

Upvotes: 0

Views: 814

Answers (3)

konkked
konkked

Reputation: 3231

You need to add paranthesis what is happening is the following:

  1. contactqueue.mail_answer_percentage is called
  2. cast is called on contactqueue.mail_answer_percentage to type GeneralPercentageQueue

Because the property mail_answer_percentage is not a property in type ContactQueue the first call fails, and you get the error that mail_answer_percentage isn't a property in ContactQueue

so your code should look like

((GeneralPercentageQueue)contactqueue).mail_answer_percentage =
    ((GeneralPercentageQueue)cq).mail_answer_percentage;

Upvotes: 1

Dustin Kingen
Dustin Kingen

Reputation: 21245

As others have mentioned you're missing parenthesis which is causing the error.

Instead you can use OfType(T) to filter the collections to only the type you want to compare.

foreach(GeneralPercentageQueue cq in p.GetGeneralEmailPercentageData(start,end)
                                      .OfType<GeneralPercentageQueue>())
{
    foreach (GeneralPercentageQueue contactqueue in finalDataList.OfType<GeneralPercentageQueue>())
    {
        if (cq.period == contactqueue.period)
        {
            contactqueue.mail_answer_percentage = cq.mail_answer_percentage;
        }
    }
}

This will prevent exceptions at runtime for mismatched types.

Upvotes: 3

argaz
argaz

Reputation: 1488

You need to add parentheses:

((GeneralPercentageQueue)contactqueue).mail_answer_percentage = ...;

Upvotes: 3

Related Questions