gpt4 book ai didi

c# - 如果当前变量为空,如何跳到下一个项目

转载 作者:行者123 更新时间:2023-11-30 19:12:13 25 4
gpt4 key购买 nike

我有一个函数可以检索设备名称列表,然后将其存储在一个变量中。然后下一步是每行使用 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 为 null,工具会崩溃,而我想做的是如果 virtuals = null 我想继续列表中的下一项。我已经尝试了一个 if 语句,但它没有按计划工作,它仍然返回 null。

最佳答案

好吧,这似乎是一个问题:

if (virtuals.Result == null)
{
i++;
getvirtuals.Location = deviceNames;

for (int v = 0; v < virtuals.Result.Count; v++)

...

如果 virtuals.Result 为 null,您希望 virtuals.Result.Count 如何工作?我怀疑你的意思是:

if (virtuals.Result != null)

但是,我怀疑你真的只是想要:

// Keep going with the next iteration of the for loop
if (virtuals == null || virtuals.Results == null)
{
continue;
}

关于c# - 如果当前变量为空,如何跳到下一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8348555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com