简体   繁体   English

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

[英]How to Fix “Not all code paths return a value”?

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! 谢谢!

The method does not return if 该方法在以下情况下不返回

stopTests == true stopTests == true

- or - - 要么 -

browserObjects == null browserObjects == null

提示:尝试找到一条没有经过return的路径。

在所有代码路径中返回值

Getter or accessor method must always return a primitive value or an object. Getter或Accessor方法必须始终返回原始值或对象。

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... 原始的,但希望有道理...

in you code you don't have else part so here i've retrn null. 在您的代码中,您没有其他部分,因此在这里我撤消了null。

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

ditch the if statement. 抛弃if语句。 technically that if statement will always hit, but compiler doesn't go that deep (code contracts would go that deep though) 从技术上讲,if语句将始终有效,但是编译器的作用不会那么深(尽管代码协定会那么深)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM