How do you make a Factory that can return derived types?

Posted by Seth Spearman on Stack Overflow See other posts from Stack Overflow or by Seth Spearman
Published on 2010-06-02T19:22:26Z Indexed on 2010/06/02 19:24 UTC
Read the original article Hit count: 570

I have created a factory class called AlarmFactory as such...

1    class AlarmFactory
2    {
3        public static Alarm GetAlarm(AlarmTypes alarmType)  //factory ensures that correct alarm is returned and right func pointer for trigger creator.
4        {
5            switch (alarmType)
6            {
7                case AlarmTypes.Heartbeat:
8                    HeartbeatAlarm alarm = HeartbeatAlarm.GetAlarm();
9                    alarm.CreateTriggerFunction = QuartzAlarmScheduler.CreateMinutelyTrigger;
10                    return alarm;
11
12                   break;
13                default:
14                
15                    break;
16            }
17        }
18    }

Heartbeat alarm is derived from Alarm. I am getting a compile error "cannot implicitly convert type...An explicit conversion exists (are you missing a cast?)". How do I set this up to return a derived type?

Seth

© Stack Overflow or respective owner

Related posts about c#

Related posts about factory