如何解决“并非所有代码路径都返回值”?

发布于 2024-09-24 12:50:26 字数 1668 浏览 2 评论 0原文

我有一个对象数组,我要返回它以在不同的方法中使用对象,但是由于某种原因,它说“并非所有代码路径都返回值”

这是代码......

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

总以为 2024-10-01 12:50:26

则该方法不会返回

如果stopTests == true

- 或 -

browserObjects == null,

The method does not return if

stopTests == true

- or -

browserObjects == null

最舍不得你 2024-10-01 12:50:26

提示:尝试找到一条不经过任何返回的路径。

Hint: try to find a path that doesn't run through any return.

凝望流年 2024-10-01 12:50:26

返回所有代码路径中的值

Return a value in all code paths

勿忘初心 2024-10-01 12:50:26

Getter 或访问器方法必须始终返回原始值或对象。

在您的示例中,您说的是

private object[] runTests(...) // Which means that no matter what, this method must return object[] or throw an exception

比您有一个条件:

if(my_condition_is_met)
{
 do_some_processing();
}

现在,如果不满足条件,那么您将不会返回任何内容,而在第一行中我们已经说过我们将返回对象数组。

原始,但希望有意义......

Getter or accessor method must always return a primitive value or an object.

In your example you are saying

private object[] runTests(...) // Which means that no matter what, this method must return object[] or throw an exception

Than you have a condition:

if(my_condition_is_met)
{
 do_some_processing();
}

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...

一绘本一梦想 2024-10-01 12:50:26

在您的代码中,您没有其他部分,因此我在这里返回 null。

    if (stopTests == false) 
    {
        //your code 
    }
    else
        return null;

in you code you don't have else part so here i've retrn null.

    if (stopTests == false) 
    {
        //your code 
    }
    else
        return null;
倥絔 2024-10-01 12:50:26
return browserObjects;     

放弃 if 语句。从技术上讲, if 语句总是会命中,但编译器不会深入(尽管代码契约会深入)

return browserObjects;     

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文