insert multi dates alarm in c#

im trying to do multi dates alarm but i don’t know the code that can make it possible

  private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime currentTime = DateTime.Now;
            DateTime alarmTime = dateTimePicker1.Value;
           


            if (currentTime.Hour==alarmTime.Hour && currentTime.Minute==alarmTime.Minute && currentTime.Second == alarmTime.Second)

            {
                timer1.Stop();
                pictureBox1.Visible = true;

in DateTime alarmTime = dateTimePicker1.Value;
i want to put more than 1 value which is dateTimePicker1 and dateTimePicker2 and dateTimePicker3 and dateTimePicker4 and dateTimePicker5
is there any way to make this ? thanks

i have tried this

 DateTime alarmTime = dateTimePicker1.Value;
 DateTime alarmTime = dateTimePicker2.Value;
 DateTime alarmTime = dateTimePicker3.Value;
 DateTime alarmTime = dateTimePicker4.Value;
 DateTime alarmTime = dateTimePicker5.Value;

but it doesnt work

"function named ‘alarmTime’ is already defined in this scope"

>Solution :

You can’t define the same object with the same name, in this case alarmTime

Do something like:

 DateTime alarmTime1 = dateTimePicker1.Value;
 DateTime alarmTime2 = dateTimePicker2.Value;
 DateTime alarmTime3 = dateTimePicker3.Value;
 DateTime alarmTime4 = dateTimePicker4.Value;
 DateTime alarmTime5 = dateTimePicker5.Value;

Or as jmcilhinney suggest, use an array or a List

List<DateTime> alarmTime = new List<DateTime>();
alarmTime.Add(dateTimePicker1.Value);
alarmTime.Add(dateTimePicker2.Value);
alarmTime.Add(dateTimePicker3.Value);
alarmTime.Add(dateTimePicker4.Value);
alarmTime.Add(dateTimePicker5.Value);

Suppose you have a list of several alarms called alarms (in plural)

List<DateTime> alarms = new List<DateTime>();   
alarms.Add(dateTimePicker1.Value);
alarms.Add(dateTimePicker2.Value);
alarms.Add(dateTimePicker3.Value);
alarms.Add(dateTimePicker4.Value);
alarms.Add(dateTimePicker5.Value);
    

Then you can go one by one doing the following (for each alarm in your list, check this…):

foreach(var alarm in alarms) {

    if (currentTime.Hour==alarm.Hour && currentTime.Minute==alarm.Minute && currentTime.Second == alarm.Second)
    {
       //your stuff

Leave a Reply