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
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?