如果当前变量为null,如何跳到下一项
本文关键字:一项 何跳 变量 null 如果 | 更新日期: 2023-09-27 18:16:18
我有一个函数,它检索设备名称列表,然后存储在变量中。然后,下一步是使用每行1个设备名称获取信息,并继续进行,直到循环完成。
String text = "";
String errors = "";
for (int i = 0; i < collection.Result.Count; i++)
{
deviceNames += collection.Result[i].DeviceName + Environment.NewLine;
getvirtuals.Location = deviceNames;
var virtuals = client.GetKnownVirtuals(getvirtuals, LtmKeyType.VirtualAddressPort);
if (virtuals.Result == null)
{
i++;
getvirtuals.Location = deviceNames;
for (int v = 0; v < virtuals.Result.Count; v++)
{
try
{
LtmKey virtualKey = new LtmKey();
virtualKey.Location = virtuals.Result[v].Location;
virtualKey.LocationType = virtuals.Result[v].LocationType;
virtualKey.Key = virtuals.Result[v].Key;
virtualKey.KeyType = LtmKeyType.VirtualAddressPort;
virtualKey.AdminGroup = admingroupComboBox.Text;
var memberStatus = client.GetMemberStatus(virtualKey);
for (int j = 0; j < memberStatus.Result.Count; j++)
{
VirtualMemberStatus status = memberStatus.Result[j];
text += String.Format("{5},{4},{0},{1},{2},{3}" + Environment.NewLine, status.Member.Address, status.Member.Port, status.EffectiveStatus, status.DesiredStatus, virtualKey.Key.Replace(":", ","), DateTime.UtcNow);
toolStripProgressBar1.PerformStep();
}
}
catch
{
errors += String.Format("{0} Error Code: 2, Error occurred, check device name (case senstive) and admin group. This error may also occur due to connection loss, try again." + Environment.NewLine, DateTime.UtcNow);
}
}
this.allResultsBox.Text = text;
getallstatusButton.Enabled = true;
}
}
我遇到的问题是,如果virtuals
为空,工具就会崩溃,而我想做的是,如果我想从列表中移动到virtuals = null
的下一个项目。我试过一个if statement
,但它没有按计划工作,它仍然是空的。
这似乎是一个问题:
if (virtuals.Result == null)
{
i++;
getvirtuals.Location = deviceNames;
for (int v = 0; v < virtuals.Result.Count; v++)
...
如果virtuals.Result
为空,您希望virtuals.Result.Count
如何工作?我怀疑你的意思是:
if (virtuals.Result != null)
然而,我怀疑你真的只是想要:
// Keep going with the next iteration of the for loop
if (virtuals == null || virtuals.Results == null)
{
continue;
}
如果你只想进入下一个循环迭代,如果virtuals为null,那么你需要if (virtuals == null) continue;
插入如何
if(virtuals == null)
continue;
就在线路之后
var virtuals = client.GetKnownVirtuals(getvirtuals, LtmKeyType.VirtualAddressPort);
您是否尝试过更改行:
if (virtuals.Result == null)
至:
if ((virtuals != null) && (virtuals.Result != null))
如果这不能解决您的问题,那么您需要指出其他错误是什么。
if (virtuals.Result == null)
制作这个
if (virtuals == null)