2010-01-12 16 views
-4

下面是一些代碼如何跳過循環coditions用於獲取功能

foreach (DataModelObject dmo in allObjects) 
{ 
    if (dmo is IResourcePolicy) 
    { 
     if (string.Compare(dmo.Name, hostName, true) == 0) 
     { 
      IResourcePolicy irp = (IResourcePolicy)dmo; 
      irp.ResourcePolicy = rp; 
      irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion); 
      irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled); 
      irp.AgentVersion = agentVersion; 

      // Distribute the object without saving it. 
      SpoServer.Spurt.ServerSendObject(dmo, true, 0); 

      break; 
     } 
    } 
} 

我想這個語句執行「irp.AgentVersion = agentVersion;」如果 (dmo是IResourcePolicy),if(string.Compare(dmo.Name,hostName,true)== 0),那麼不執行這三個循環「foreach(所有對象中的DataModelObject dmo)我想在循環內執行整個四個任務 ,包括以前的任務(irp.AgentVersion = agentVersion;)。 以前在UI顯示其沒有不執行環,,執行一次 顯示所有值,,我們需要改變

誰能給要執行的代碼這個邏輯,,有「GOTO」循環條件檢查,我們可以在這裏做

+2

我只看到一個循環,而不是三個。 – RedFilter

+0

一個foreach和兩個if循環 – peter

+2

我不認爲IF語句被認爲是一個循環... – auujay

回答

4

我相信你正在尋找continue

if (dmo is IResourcePolicy) 
{ 
    etc... 
} 
else 
{ 
    continue; 
} 

編輯:

基礎上的評論,這裏是我明白你想做的事:

另外要注意,只有一個循環這裏,和你打破一旦你的內在條件得到滿足,它就會離開它。我認爲這可能會讓你感到困惑。現在的方式是,您始終只處理收藏中的其中一個對象。

以下將刪除break語句,以便處理集合中的每個對象。

foreach (DataModelObject dmo in allObjects) 
{ 
    if (dmo is IResourcePolicy) 
    { 
     // if these loops are not executed i want to show agentversion instead of showing None in UI layer 
     IResourcePolicy irp = (IResourcePolicy)dmo; 
     irp.AgentVersion = agentVersion; 

     //(else) i want to show the entire four things including agent version 
     if (string.Compare(dmo.Name, hostName, true) == 0) 
     {    
      irp.ResourcePolicy = rp; 
      irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion); 
      irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled); 
     } 

     // Distribute the object without saving it. 
     SpoServer.Spurt.ServerSendObject(dmo, true, 0); 
    } 
} 
+0

實際上我需要的是,如果這些循環未執行,我想顯示agentversion而不是在UI層中顯示None。如果循環被執行,我想要顯示包括代理版本在內的整個四件事情,我不想在現有的三個循環中放置任何其他部分 – peter

+0

我怎樣才能繼續使用這裏雖然有三個循環 – peter

+2

繼續僅在功能上循環操作(while,for,foreach ....我是否忘記了一個...)爲if條件定義的範圍(或'{/ * code here/*}')不受循環運算符影響。因此,continue語句將導致continue語句之後的任何代碼被忽略,並且循環繼續下一個值。 –

-1

我不清楚你在做什麼。這是關閉嗎?

foreach (DataModelObject dmo in allObjects) 
{ 
    if (dmo is IResourcePolicy) 
    { 
     IResourcePolicy irp = (IResourcePolicy)dmo; 
     irp.AgentVersion = agentVersion; 

     if (string.Compare(dmo.Name, hostName, true) == 0) 
     { 
      irp.ResourcePolicy = rp; 
      irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion); 
      irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled); 
     } 

     // Distribute the object without saving it. 
     SpoServer.Spurt.ServerSendObject(dmo, true, 0); 

     break; 
    } 
} 
+0

沒有這個不工作,我應該需要顯示循環執行前的代理版本,如果循環執行,我也需要顯示包括代理版本的所有值 – peter

2

這是一個有點難以破譯你要找什麼,但我會在它採取了刺:

bool objectsFound = false; 
foreach (DataModelObject dmo in allObjects) 
{ 
    if (dmo is IResourcePolicy && string.Compare(dmo.Name, hostName, true) == 0) 
    { 
     // ... 
     objectsFound = true; 
    } 
} 

if(objectsFound) 
{ 
    // "show the entire four things including agent version" 
} 
else 
{ 
    // " show agentversion instead of showing None in UI layer" 
} 
+0

讓我試試這個 – peter

+0

,但我得到錯誤,,錯誤使用未分配的局部變量'objectsFound' – peter

+1

@peter替換「bool objectsFound;」用「bool objectFound = false;」 – bniwredyc

1

您可以消除你的循環,並通過使用一些LINQ嵌套的if語句。這裏的一般想法:

var objects = new List<Object>(); 
objects.Add(1); 
objects.Add("string"); 
objects.Add("magic"); 
objects.Add(2.5); 

var magic = (from o in objects 
      where o is string 
       && ((string)o) == "magic" 
      select o as string).SingleOrDefault(); 

if(magic != null) { 
    Console.Write("magic found: {0}", magic); 
} 
else { 
    // Do your other logic if nothing was found (loop, etc) 
} 
+0

我使用2.0框架而不是3.5 – peter

+1

這是不幸的。有兩種方法可以在2.0中使用vanilla LINQ,但如果你感興趣的話:http://stackoverflow.com/questions/2138/linq-on-the-net-2-0-runtime –

+1

LINQBridge將被推薦:http:///www.albahari.com/nutshell/linqbridge.aspx。正如其作者所述,這個問題被接受的答案是「黑客」。提到LINQBride,但稍後在頁面中。 –

1

這是我將如何重寫該代碼,如果我有.NET 3.5或更好的豪華。沒有循環,一個if聲明。

var irp = allObjects.OfType<IResourcePolicy>() 
    .FirstOrDefault(item => String.Equals(item.Name, hostName)); 

if (irp != null) 
{ 
    irp.ResourcePolicy = rp; 
    irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion); 
    irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled); 
    irp.AgentVersion = agentVersion; 

    // I don't know the signature of ServerSendObject, 
    // you might need a cast here: 
    SpoServer.Spurt.ServerSendObject(irp, true, 0); 
}