我有八個對象是超類的子類。他們都擁有相同的領域和屬性。我想要做的是以某種方式遍歷這組對象,並找到其狀態字段設置爲false的第一個,然後更新與該對象關聯的其他字段。對一堆對象的迭代
我一直在試圖用一串if語句來實現這一點。它不是很漂亮,它只能通過一組對象工作一次。
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();
}
有沒有更好的方法來實現我期待的目標?我用c#約兩週的時間,所以我爲我的無知道歉。
編輯:看起來我沒有足夠的解釋。
背景:我有一小塊技術叫做BlinkStick,我試圖通過一個加載項使它成爲Outlook的通知燈。我想要發生的是當第一封電子郵件進入它打開燈1.然後,當第二封電子郵件進來它打開燈2,等等。當電子郵件的狀態從未讀到讀取,然後它轉動燈與它相關聯。如果有其他燈光處於活動狀態,則會將這些燈光移至上一個空間。所以Light 2會移動到Light 1,Light 3會移動到Light 2。
這裏是所有燈都基於的類。
class LightSuper
{
public LightSuper()
{
}
public LightSuper(byte i, bool e)
{
i = index;
e = Status;
}
private static string email;
public static string Email
{
get { return email; }
set { email = value; }
}
private static byte index;
public static int Index
{
get { return index; }
set { index = Convert.ToByte(value+1); }
}
private static bool status;
public static bool Status
{
get { 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()
{
}
}
我發現,如果我創建超輕的新實例,每一次的電子郵件進來我不能直接編輯的具體實例,並仍然能夠更新lightLoop while循環不打開所有的燈關閉。所以我創建了八個子類,以便每個類對象都可以單獨編輯。
如果有更好的方法來做到這一點,我是全部耳朵。
將在這種情況下更好地開關案件工作..這是一些凌亂的代碼btw修復格式 – MethodMan
這裏的小方面說明,因爲你開始C#這將是一個很好的時間來拿起約定。例如,變量名應該在PascalCase中的camelCase和方法中。 –
如果他們_all_共享_same字段和屬性_,那麼您可能只需製作該超類的多個實例,而不是爲您需要的每個燈創建不同的類。 –