2015-06-19 75 views
0

我有八個對象是超類的子類。他們都擁有相同的領域和屬性。我想要做的是以某種方式遍歷這組對象,並找到其狀態字段設置爲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循環不打開所有的燈關閉。所以我創建了八個子類,以便每個類對象都可以單獨編輯。

如果有更好的方法來做到這一點,我是全部耳朵。

+0

將在這種情況下更好地開關案件工作..這是一些凌亂的代碼btw修復格式 – MethodMan

+2

這裏的小方面說明,因爲你開始C#這將是一個很好的時間來拿起約定。例如,變量名應該在PascalCase中的camelCase和方法中。 –

+1

如果他們_all_共享_same字段和屬性_,那麼您可能只需製作該超類的多個實例,而不是爲您需要的每個燈創建不同的類。 –

回答

0

使用LINQ,與一個特定類的集合。 例子:

var lights = new List<Light>(); 
//Insert logic to add lights to the collection (eg. lights.Add(new Light());) 
var firstLight = lights.FirstOrDefault(light => !light.Status); 
if (firstLight != null) 
{ 
    firstLight.Email = newId; 
    firstLight.Status = true; 
    firstLight.Index = Id.IndexOf(newId); 
    firstLight.lightStart(); 
    firstLight.Status = true; 
} 
2

LINQ是這類任務的一大利器。您的代碼可能是這個樣子:

List<SuperClass> lightList = {...} //Add light1, light2, etc to the list. 
SuperClass light = lightList.FirstOrDefault(l => !l.Status); 
if (light != null) 
{ 
    light.Email = newId; 
    //Set rest of proprties 
} 

或者,如果你想更新列表中的所有對象,其中狀態==假

List<SuperClass> lightList = {...} //Add light1, light2, etc to the list. You may also want to order this list? 
foreach(SuperClass light in lightList.Where(l => !l.Status)) 
{ 
    light.Email = newId; 
    //Set rest of proprties 
} 
1

與片斷的邏輯問題是,如果light7的狀態是false那麼它將轉到嵌套的if,它不會到達else語句,它將設置相應的信息,這就是你想要的。所以,如果所有的狀態都false,只有Light0將其屬性改變。從邏輯上講,你會想你的if語句來沒有在這種情況下!因爲如果他們是真的,你不希望這樣的改變光的屬性做,你只是想檢查下一個。

此外,它看起來像你的燈是每一個人類,每一類具有完全相同的特性。我一定會研究如何創建一個單獨的類並從該類創建對象。在對象上

文檔:

https://msdn.microsoft.com/en-us/library/ms173110.aspx

我會考慮循環和集合,特別是for環和lists

文檔兩種:

https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/ch45axte.aspx

可以整個代碼段降低到約10行。

我先在列表中創建一個你的對象的「集合」,例如:

List<Light> lights = new List<Light>;

然後,你的光對象添加到列表:

lights.add(light)每次更換light點亮並複製每行添加的行。

然後你想在列表中的「迭代」或循環每個對象,像這樣:

foreach (int index = 0; index < lights.Count()-1; index++) 
{ 
    if (!lights[i].Status) 
    { 
      lights[i].Email = newId; 
      lights[i].Status = true; 
      lights[i].Index = Id.IndexOf(newId); 
      lights[i].lightStart(); 
      lights[i+1].Status = true; 
      break; 
    } 
} 

在這種情況下,你會希望! if語句,因爲如果狀態false它將進入if語句,因爲!將條件結果轉換爲true。反之亦然,如果屬實,它將變成錯誤,並繼續經過循環。

if語句塊的最後一行中的break;語句將阻止我們通過列表進行搜索,因爲它將'中斷'出循環。