Logic problem while setting conditions

前端 未结 1 434
渐次进展
渐次进展 2021-01-28 14:55

i have a requirement This ‘if’ structure must be changed. As it is currently coded the agent version is only set when the policy GUID sent by RM matches a GUID of a po

1条回答
  •  粉色の甜心
    2021-01-28 15:30

    I think what you mean is:

    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);
            }
    
            // ...
        }
    }
    

    I've removed the else bit so the loop always gets executed, then added if (rp != null) inside the loop that prevents some part of it from executing. That way you don't have to duplicate the loop code itself, which is what I think you were doing?

    0 讨论(0)
提交回复
热议问题