我有一个从组中删除用户的功能。
首先,它检查组中是否有任何用户。然后在for循环中,它遍历所有用户并检查输入的当前用户是否存在于组中。
第一次运作。它删除了用户。但是第二次用户不再存在而且它出现在else语句中。
如何在删除用户时结束for循环?
以下是代码:
for (int y = 0; y < cognosGroup.members.value.Length; y++)
{
//check if member[y] is the one that need to be deleted
if (cognosGroup.members.value[y].searchPath.value == member[0].searchPath.value)
{
int lenght = cognosGroup.members.value.Length - 1;
baseClass[] newMembers = new baseClass[lenght];
int index = 0;
baseClass obj = null;
// go trough group
for (int i = 0; i <= lenght; i++)
{
if (i != y)
{
//create user
obj = cognosGroup.members.value[i];
newMembers[index] = obj;
index++;
}
else
{
this._messageText = "*Succesfully removed " + this.RemoveUsername.ToLower() + " from " + this.DataViewModel.GroupModel.SelectedGroup;
this.ExHandling = new ExceptionHandling(this._messageText);
this.MessageText = this.ExHandling.ExHandlingOkey;
}
}
cognosGroup.members = new baseClassArrayProp();
cognosGroup.members.value = newMembers;
this.LogonModel.CBICMS.update(new baseClass[] { cognosGroup }, new updateOptions());
y--;
}
else
{
this._messageText = "*" + this.RemoveUsername.ToLower() + " is not a user from " + this.DataViewModel.GroupModel.SelectedGroup;
this.ExHandling = new ExceptionHandling(this._messageText);
this.MessageText = this.ExHandling.ExHandlingError;
}
}
答案 0 :(得分:3)
只需使用return语句退出例程或继续跳到下一次迭代。