如果数组有一些未设置的值,如何设置返回值
我有这样的代码
int rpindex = allObjects.Find(new Guid(policyGuid));
if (rpindex != -1)
{
rp = (ResourcePolicy)allObjects.GetAt(rpindex);
}
,这是 Find 方法
public virtual int Find(Guid guid)
{
try
{
for (int i=0; i<this.Count; i++)
{
if (guid.Equals(this[i].Guid))
{
return i;
}
}
}
catch(System.Exception exception)
{
SpoDebug.DebugTraceSevere(func, "Exception Occurred: " + exception.ToString() );
}
SpoDebug.DebugTraceVerbose(func, "no find. guid=" + guid.ToString());
return -1;
}
截至目前,现有函数 Find() 结果是 -1 或某个整数值[i]。 -1 值在两种情况下出现,即如果输入为空 [ null 值],并且如果输入是不在数据库或当前列表中的某个值,我需要在此处进行更改。这意味着如果输入当 Find() 为空或 null 时,它应该返回 -1,否则如果输入有某个值并且不匹配,那么它会返回 -2。所以应该有三个结果,第一个是 -1,第二个是 -2第三个是整数值,任何人都可以在这里指导我吗 如果我添加 else 循环,我不确定除了 -1 和整数值之外我可以在这里使用什么返回值
I have a code like this
int rpindex = allObjects.Find(new Guid(policyGuid));
if (rpindex != -1)
{
rp = (ResourcePolicy)allObjects.GetAt(rpindex);
}
and this is Find method
public virtual int Find(Guid guid)
{
try
{
for (int i=0; i<this.Count; i++)
{
if (guid.Equals(this[i].Guid))
{
return i;
}
}
}
catch(System.Exception exception)
{
SpoDebug.DebugTraceSevere(func, "Exception Occurred: " + exception.ToString() );
}
SpoDebug.DebugTraceVerbose(func, "no find. guid=" + guid.ToString());
return -1;
}
As of now the existing function Find() outcome is -1 or some integer value[i]. The -1 value will come in two situations , that is if the input is empty [ null value] and if the input is some value which is not in the database or in the current list, i need change here.That mean if the input to Find() is empty or null that time only it should return -1, otherwise if input has some value and it is not maching then it shud return return -2.SO there should be three outcomes one is -1 second is -2 and third is integer value ,Can any body guide me here
if i add else loop, i am not sure what return value i can use here other than -1, and integer value
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需放置额外的返回语句,或者我遗漏了什么?
IE
Just place extra return statements, or am I missing something?
i.e.
就在 for 循环检查之后
just after the for loop check
我认为在方法开始之后,如果列表为空,则抛出异常会更具可读性:
有多个整数值表示错误是非常不清楚的。
I think it will be more readable to throw an exception in case the list is empty, right after the start of the method:
Having more than one integer value for error is very unclear.