本文介绍了设置条件时出现逻辑问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个要求必须更改此如果"结构.由于当前已编码代理版本,因此仅在以下情况下设置代理版本:RM匹配服务器上策略的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;
所以我做了什么,我在if循环(if(rp == null))之外进行了赋值(irp.AgentVersion = agentVersion;)
so what i did i made the assignment (irp.AgentVersion = agentVersion;) outside if loop ( if (rp == null))
像这样但是我没有得到版本值
foreach (DataModelObject dmo in allObjects)
{
if (dmo is IResourcePolicy)
{
if (string.Compare(dmo.Name, hostName, true) == 0)
{
irp.AgentVersion = agentVersion;
}
有人可以建议我在这里做什么
Can any one suggest me what i have to do here
推荐答案
我认为您的意思是:
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)
,以防止其中的一部分执行.这样一来,您不必重复循环代码本身,这就是我认为您正在做的事情?
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?
这篇关于设置条件时出现逻辑问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!