Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 837453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:08:35+00:00 2026-05-15T05:08:35+00:00

I have created a factory class called AlarmFactory as such… 1 class AlarmFactory 2

  • 0

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?

EDIT

THANK YOU ALL FOR YOUR ANSWERS. I fixed the compile error within ten minutes which is why i did not post the whole error. But I appreciated the different approaches that were mentioned.

For the record it was “Cannot implicitly convert type ‘goAlarmsCS.HeartbeatAlarm’ to ‘goAlarmsCS.Alarm’ An explicit conversion exists (are you missing a cast?)”. (I think.) The error was occurring on line 8.

Seth

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:08:36+00:00Added an answer on May 15, 2026 at 5:08 am

    Below is a solution that includes a specific GetHeartbeatAlarm function to retrieve a HeartbeatAlarm object as well as a generic GetAlarm function to return an alarm whose type is determined by the generic parameter. At the bottom there is some example code showing how this would be called:

    enum AlarmTypes
    {
        Heartbeat,
        AnotherAlarm,
        // ...
    }
    
    delegate void CreateTriggerDelegate();
    
    class Alarm
    {
        // ...
    
        public CreateTriggerDelegate CreateTriggerFunction { get; set; }
    }
    
    class HeartbeatAlarm : Alarm
    {
        // ...
    
        public static HeartbeatAlarm GetAlarm()
        {
            return new HeartbeatAlarm();
        }
    }
    
    class QuartzAlarmScheduler
    {
        public static CreateTriggerDelegate CreateMinutelyTrigger { get; set; }
    }
    
    class AlarmFactory
    {
        public static Alarm GetAlarm(AlarmTypes alarmType)  //factory ensures that correct alarm is returned and right func pointer for trigger creator.
        {
            switch (alarmType)
            {
                case AlarmTypes.Heartbeat:
                    return GetHeartbeatAlarm();
                default:
                    throw new ArgumentException("Unrecognized AlarmType: " + alarmType.ToString(), "alarmType");
            }
        }
    
        static Alarm _GetAlarm<T>()
            where T : Alarm
        {
            Type type = typeof(T);
            if (type.Equals(typeof(HeartbeatAlarm)))
                return GetHeartbeatAlarm();
            else
                throw new ArgumentException("Unrecognized generic Alarm argument: " + type.FullName, "T");
        }
    
        public static T GetAlarm<T>()
            where T : Alarm
        {
            return (T)_GetAlarm<T>();
        }
    
        public static HeartbeatAlarm GetHeartbeatAlarm()
        {
            HeartbeatAlarm alarm = HeartbeatAlarm.GetAlarm();
            alarm.CreateTriggerFunction = QuartzAlarmScheduler.CreateMinutelyTrigger;
            return alarm;
        }
    }
    
    class Example
    {
        static void GetAlarmExamples()
        {
            HeartbeatAlarm alarm;
    
            alarm = AlarmFactory.GetHeartbeatAlarm();
    
            alarm = AlarmFactory.GetAlarm<HeartbeatAlarm>();
    
            alarm = (HeartbeatAlarm)AlarmFactory.GetAlarm(AlarmTypes.Heartbeat);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user object called UserSystem, which is created by a static factory
i have created a simple public ref class in the vc++ project, which is
I have an Object Factory Class which has a method called registerCreator as defined
Inside a container object, I have created a class Factory , that is responsible
I have a wrapper class for the Bitmap class called BitmapZone. Assuming we have
I have a utility class library I am writing. A have written a factory
I have two test classes called TT_Common and TT_Container which extends CPPUNIT_NS::TestFixture: class TT_Common
I have a simple class that has a property called executor in which I'd
In apache.commons.collections there is a class called MapUtils which have these two methods to
I have a Class called MyMailSender that binds to the asp.net controller called EmailController.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.