none
How to name an instance of timer class at runtime RRS feed

  • Question

  • I made a class named AppTimer which is inherited from Timer class.

    I want to create instance of it dynamically with List<AppTimer> type.

    How to do it?

    My ultimate goal is to handle event of it dynamically.

    Friday, April 12, 2019 5:33 AM

Answers

  • You can't do that. The nearest you can get is to use a list or array of timers and access them via the index.

    apptTimer[m_appt_no] or apptTimer[1] or whatever.

    • Marked as answer by Jeff0803 Monday, April 15, 2019 8:49 PM
    Monday, April 15, 2019 4:12 AM

All replies

  • Hi,

    You can achieve the requirement via reflection. And here is an example you can follow:

        namespace ReflectionTest
        {
            public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
                }
                public void CreateInstance()
                {
                    // Get the current assembly
                    Assembly assembly = Assembly.GetExecutingAssembly();
                    // Without params
                    object obj1 = assembly.CreateInstance("ReflectionTest.AppTimer", true);
                    // Convert to "AppTimer"
                    AppTimer appTimer1 = (AppTimer)obj1;
                    appTimer1.Property1 = "111";
                    MessageBox.Show(appTimer1.Property1);
    
                    // With params
                    Object[] param = new Object[2];
                    param[0] = "1";
                    param[1] = "2";
                    Object obj2 = assembly.CreateInstance("ReflectionTest.AppTimer", true, BindingFlags.Default, null, param, null, null);
                    // Convert to "AppTimer"
                    AppTimer appTimer2 = (AppTimer)obj2;
                    MessageBox.Show(appTimer2.Property1 + ", " + appTimer2.Property2);
                }
    
                private void Form1_Load(object sender, EventArgs e)
                {
                    CreateInstance();
                }
            }
            public class AppTimer : Timer
            {
                public string Property1 { get; set; }
                public string Property2 { get; set; }
    
                public AppTimer() { }
    
                public AppTimer(string property1, string property2)
                {
                    Property1 = property1;
                    Property2 = property2;
                }
            }
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 12, 2019 8:06 AM
  • Why would you suggest that, rather than the obvious method:

        AppTimer appTimer2 = new AppTimer(1,2);

    I'm 99% certain that's the answer the original poster needs.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Friday, April 12, 2019 8:58 PM
  • My purpose is to name timer related with ApptNo listed following sample code.

    I have a list, List<Appointment>.

    Appointment is a class like following.

    Class Appointment
    
    {
         int m_appt_no;
         .
         .
         public int ApptNo
        {
            get;set;
        }
         .
         .
    }

    I want to created timer named apptTimer_1, apptTimer_2...(1, 2,... are ApptNo) to manage timer easily and dynamically.

    How can I do this?



    • Edited by Jeff0803 Monday, April 15, 2019 4:02 AM
    Monday, April 15, 2019 4:01 AM
  • You can't do that. The nearest you can get is to use a list or array of timers and access them via the index.

    apptTimer[m_appt_no] or apptTimer[1] or whatever.

    • Marked as answer by Jeff0803 Monday, April 15, 2019 8:49 PM
    Monday, April 15, 2019 4:12 AM
  • Is it good idea to add Timer into the class like AppointmentS and manage timer through instance of the List<AppointmentS>?

    public class AppointmentS{
    {
        Appointment appointment;
        Timer timer;
    }
    
    List<AppointmentS> appointmentlist = new List<AppointmentS>;
    

    I'm not sure if this is a good idea and possible to manage events of Timer.


    • Edited by Jeff0803 Monday, April 15, 2019 11:15 PM
    Monday, April 15, 2019 8:53 PM