tags:

views:

215

answers:

6

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!

+4  A: 

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

xtofl
+16  A: 

The method does not return if

stopTests == true

- or -

browserObjects == null

TheNextman
...and just to clarify - every method in C# that returns a value **must** have an explicit `return` statement (or a thrown exception) at the end of every code path.
Michael Petrotta
@Michael: Or an infinite loop; the compiler will treat "while(true){}" as having an unreachable end point. It is actually easier to say what you mean by phrasing it as: **the end point of a non-void method must not be reachable**.
Eric Lippert
@Eric: yes, that makes a lot of logical sense, but I'm not sure how prevalent the concept of an method's "end point" is.
Michael Petrotta
@Eric you mean must be?
Nico
@Michael: The end-point of the method is lexically the close curly brace that terminates the body; I don't think that's a particularly abstruse concept.
Eric Lippert
@Nico: No, I mean what I said. Let me re-state it. If the end point of a non-void method is reachable then the method can terminate without returning a value. It is not legal for a non-void method to terminate without returning a value, except via an exception. Therefore the end point of a non-void method must not be reachable. Is that now clear?
Eric Lippert
@Eric, ah. I didn't understand the "end point" was the closing brace tag }
Nico
+1  A: 
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)

Darren Kopp
+3  A: 

Return a value in all code paths

Nico
+1  A: 

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

vikp
+1  A: 

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

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