0
public bool function()
{
    bool doesExist = false;          
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += (o, ea) =>
        {
            // some work done here
        };

    worker.RunWorkerCompleted += (o, ea) =>
        {
            //somw logic here
            return doesExist;                               
        };
} 

我想要doesExist值作为函数的返回值,但我收到智能感知错误

system.componentmodel。runworkercompletedeventhandler 只返回 void,return 关键字后面不能跟对象表达式

为什么我收到此错误,如何返回布尔值?

4

1 回答 1

2
public bool function()
{
   bool doesExist = false;          
   BackgroundWorker worker = new BackgroundWorker();

   worker.DoWork += (o, ea) =>
            {
                 // do some work
                 ea.Result = true; // set this as true if all goes well!
            };

    worker.RunWorkerCompleted += (o, ea) =>
        {
              // since the boolean value is calculated here &
              // RunWorkerCompleted returns void
              // you can create a method with boolean parameter that handle the result.
              Another_Way_To_Return_Boolean(doesExist)


        };
} 


private void Another_Way_To_Return_Boolean(bool result)
{
    if (result)
    {

    }
}
于 2013-03-04T10:26:46.750 回答