2010-01-08 38 views
0

我有要求必須更改此'if'結構。由於目前編碼,代理版本僅在 RM發送的策略GUID與服務器上策略的GUID匹配時才設置。我們總是要設置的版本無論的GUID匹配的。(什麼都可以的情況)設置條件時的邏輯問題

這裏是編碼

ResourcePolicy rp = null; 
try 
{ 
    int rpindex = allObjects.Find(new Guid(policyGuid)); 
    if (rpindex != -1) 
    { 
    rp = (ResourcePolicy)allObjects.GetAt(rpindex); 
    } 
} 
catch (System.Exception err) 
{ 
    SpoDebug.DebugTraceSevere(func, "Bad GUID: " + policyGuid + " Exception: " + err.Message); 
    rp = null; 
} 

if (rp == null) // this the if loop we need to concentrate 
{ 
    SpoDebug.DebugTraceSevere(func, "Unable to find ResourcePolicy with GUID: " + policyGuid); 
} 
else 
{ 
    // Search for the specified host 
    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; 

所以我做了什麼我完成了任務(irp.AgentVersion = agentVersion;)外,如果循環(如果(RP == NULL))

像這樣但我沒有得到版本值

foreach (DataModelObject dmo in allObjects) 
{ 
if (dmo is IResourcePolicy) 
{ 
    if (string.Compare(dmo.Name, hostName, true) == 0) 
    { 
    irp.AgentVersion = agentVersion; 
    } 

任何一個可以建議我什麼,我要在這裏做

回答

0

我想你的意思是:

ResourcePolicy rp = null; 
try 
{ 
    int rpindex = allObjects.Find(new Guid(policyGuid)); 
    if (rpindex != -1) 
    { 
     rp = (ResourcePolicy)allObjects.GetAt(rpindex); 
    } 
} 
catch (System.Exception err) 
{ 
    SpoDebug.DebugTraceSevere(func, "Bad GUID: " + policyGuid + " Exception: " + err.Message); 
} 

if (rp == null) // this the if loop we need to concentrate 
{ 
    SpoDebug.DebugTraceSevere(func, "Unable to find ResourcePolicy with GUID: " + policyGuid); 
} 

// Search for the specified host 
foreach (DataModelObject dmo in allObjects) 
{ 
    if (dmo is IResourcePolicy && string.Compare(dmo.Name, hostName, true) == 0)) 
    { 
     IResourcePolicy irp = (IResourcePolicy)dmo; 
     irp.AgentVersion = agentVersion; 

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

     // ... 
    } 
} 

我已經刪除了else位,因此該循環總是被執行,然後加入if (rp != null)內循環,阻止它的一部分執行。這樣你就不必複製循環代碼本身,這是我認爲你在做什麼?

+0

根據邏輯,這將工作,但那段時間也無法看到版本,只有在分配資源政策版本後即將到來 – peter 2010-01-08 10:03:21