c# - iteration over a bunch of objects -
i have 8 objects sub classes of super class. share same fields , properties. wanting somehow iterate on group of objects , find first 1 status field set false update other fields associated object.
i have been trying accomplish string of if statements. not pretty , works 1 time through group of objects.
if (!light7.status) { if (!light6.status) { if (!light5.status) { if (!light4.status) { if (!light3.status) { if (!light2.status) { if (!light1.status) { if (!light0.status) { light0.email = newid; light0.status = true; light0.index = id.indexof(newid); light0.lightstart(); light1.status = true; } } else { light1.email = newid; light1.status = true; light1.index = id.indexof(newid); light1.lightstart(); light2.status = true; } } else { light2.email = newid; light2.status = true; light2.index = id.indexof(newid); light2.lightstart(); light3.status = true; } } else { light3.email = newid; light3.status = true; light3.index = id.indexof(newid); light3.lightstart(); light4.status = true; } } else { light4.email = newid; light4.status = true; light4.index = id.indexof(newid); light4.lightstart(); light5.status = true; } } else { light5.email = newid; light5.status = true; light5.index = id.indexof(newid); light5.lightstart(); light6.status = true; } } else { light6.email = newid; light6.status = true; light6.index = id.indexof(newid); light6.lightstart(); light7.status = true; } } else { light7.email = newid; light7.status = true; light7.index = id.indexof(newid); light7.lightstart(); }
is there better way achieve looking do? 2 weeks journey c# apologize ignorance.
edit: looks didn't explain enough.
background: have little piece of technology called blinkstick , trying make notification light outlook via add-in. want happen when first email comes in turns on light 1. when second email comes in turns on light 2, etc. when status of email goes unread read turns of light has been associated it. if there other lights active moves lights previous space. light 2 move light 1, , light 3 move light 2 example.
here class lights based off of.
class lightsuper { public lightsuper() { } public lightsuper(byte i, bool e) { = index; e = status; } private static string email; public static string email { { return email; } set { email = value; } } private static byte index; public static int index { { return index; } set { index = convert.tobyte(value+1); } } private static bool status; public static bool status { { return status; } set { status = value; } } public static void lightstart() { thread start = new thread(() => lightloop(ref index, ref status)); start.isbackground = true; start.start(); } private static void lightloop(ref byte index, ref bool state) { stopwatch timer = new stopwatch(); blinkstick device = blinkstick.findfirst(); if (device != null && device.opendevice()) { timer.start(); while (state) { if (timer.elapsed.minutes < 3) { device.setcolor(0, index, "green"); thread.sleep(500); device.setcolor(0, index, "black"); thread.sleep(500); } if (timer.elapsed.minutes >= 3 && timer.elapsed.minutes < 5) { device.setcolor(0, index, "yellow"); thread.sleep(375); device.setcolor(0, index, "black"); thread.sleep(375); } if (timer.elapsed.minutes >= 5) { device.setcolor(0, index, "red"); thread.sleep(250); device.setcolor(0, index, "black"); thread.sleep(250); } } } } } class light0 : lightsuper { public light0() { } } class light1 : lightsuper { public light1() { } } class light2 : lightsuper { public light2() { } } class light3 : lightsuper { public light3() { } } class light4 : lightsuper { public light4() { } } class light5 : lightsuper { public light5() { } } class light6 : lightsuper { public light6() { } } class light7 : lightsuper { public light7() { } }
i found if create new instance of superlight every single time email comes in couldn't edit specific instance directly , still able update while loop in lightloop without turning lights off. created 8 sub classes each class object can edited individually.
if there better way ears.
linq great tool kind of task. code might this:
list<superclass> lightlist = {...} //add light1, light2, etc list. superclass light = lightlist.firstordefault(l => !l.status); if (light != null) { light.email = newid; //set rest of proprties }
or, if wanted update objects in list status == false
list<superclass> lightlist = {...} //add light1, light2, etc list. may want order list? foreach(superclass light in lightlist.where(l => !l.status)) { light.email = newid; //set rest of proprties }
Comments
Post a Comment