如何解决“并非所有代码路径都返回值”?
我有一个对象数组,我要返回它以在不同的方法中使用对象,但是由于某种原因,它说“并非所有代码路径都返回值”
这是代码......
private object[] runTests(string banText, object tabControlName, int runThisTest, string testName)
{
if (stopTests == false)
{
var tabPageBrowser = new TabPage();
var Browser = new WebBrowser();
(tabControlName as TabControl).TabPages.Add(tabPageBrowser);
tabPageBrowser.Name = tabControlName.ToString();
tabPageBrowser.Text = testName;
tabPageBrowser.Font = new System.Drawing.Font("Trebuchet MS", 8.25F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
Browser.Dock = DockStyle.Fill;
Browser.Url = new Uri(testStrings(runThisTest, banText));
Browser.Name = tabControlName.ToString();
Browser.ScriptErrorsSuppressed = true;
tabPageBrowser.Controls.Add(Browser);
try
{
while (Browser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
}
catch
{
return null;
}
IntPtr pHandle = GetCurrentProcess();
SetProcessWorkingSetSize(pHandle, -1, -1);
object[] browserObjects = new object[2];
browserObjects[0] = tabPageBrowser;
browserObjects[1] = Browser;
if (browserObjects != null)
{
return browserObjects;
}
}
}
我不明白我做错了什么,你能帮忙吗?谢谢你!
I have an object array that i'm returning to use the objects in a different method, however for some reason it's saying "not all code paths return a value"
Here's the code....
private object[] runTests(string banText, object tabControlName, int runThisTest, string testName)
{
if (stopTests == false)
{
var tabPageBrowser = new TabPage();
var Browser = new WebBrowser();
(tabControlName as TabControl).TabPages.Add(tabPageBrowser);
tabPageBrowser.Name = tabControlName.ToString();
tabPageBrowser.Text = testName;
tabPageBrowser.Font = new System.Drawing.Font("Trebuchet MS", 8.25F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
Browser.Dock = DockStyle.Fill;
Browser.Url = new Uri(testStrings(runThisTest, banText));
Browser.Name = tabControlName.ToString();
Browser.ScriptErrorsSuppressed = true;
tabPageBrowser.Controls.Add(Browser);
try
{
while (Browser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
}
catch
{
return null;
}
IntPtr pHandle = GetCurrentProcess();
SetProcessWorkingSetSize(pHandle, -1, -1);
object[] browserObjects = new object[2];
browserObjects[0] = tabPageBrowser;
browserObjects[1] = Browser;
if (browserObjects != null)
{
return browserObjects;
}
}
}
I dont see what i'm doing wrong, can you please assist? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
则该方法不会返回
如果stopTests == true
- 或 -
browserObjects == null,
The method does not return if
stopTests == true
- or -
browserObjects == null
提示:尝试找到一条不经过任何
返回
的路径。Hint: try to find a path that doesn't run through any
return
.返回所有代码路径中的值
Return a value in all code paths
Getter 或访问器方法必须始终返回原始值或对象。
在您的示例中,您说的是
比您有一个条件:
现在,如果不满足条件,那么您将不会返回任何内容,而在第一行中我们已经说过我们将返回对象数组。
原始,但希望有意义......
Getter or accessor method must always return a primitive value or an object.
In your example you are saying
Than you have a condition:
Now, if condition isn't met, than you are not going to return anything, when in the first line we've said that we are going to return array of objects.
Primitive, but hopefully makes sense...
在您的代码中,您没有其他部分,因此我在这里返回 null。
in you code you don't have else part so here i've retrn null.
放弃 if 语句。从技术上讲, if 语句总是会命中,但编译器不会深入(尽管代码契约会深入)
ditch the if statement. technically that if statement will always hit, but compiler doesn't go that deep (code contracts would go that deep though)