2011-07-13 47 views
2
return found; 

'迴歸' 所賜:無效令牌類,結構或接口成員聲明

Invalid token 'return' in class, struct, or interface member declaration 

代碼:

public bool IsTextPresent(ISelenium Selenium, string searchText) 
    { 

     int count = (int)Selenium.GetXpathCount("//*[@id='resultTable']/table"); 
     string text; 

     bool found; 

     for (int i = 1; i <= count; i++) 
     { 
      StringBuilder container = new StringBuilder(); 

      for (int j = 4; j <= 8; j += 2) 
      { 

       if (Selenium.IsElementPresent("xpath=/html/body/form/div[2]/table[" + (i) + "]/tr[" + (j) + "]/td[4]")) 
       { 
        text = Selenium.GetText("xpath=/html/body/form/div[2]/table[" + (i) + "]/tr[" + (j) + "]/td[4]"); 


        container.Append(text + Environment.NewLine); 
       } 

       string fullText = container.ToString(); 
        if (!fullText.Contains(searchText)) 
        { 
         found = false; 
        } 
        else 
        { 
         found = true; 
        } 
       } 
      } 
     } 
     return found;    
    } 
+0

我知道正在處理的問題是大括號,但我會質疑這種方法的命名。如果在每個'xpath =/html/body/form/div [2]/table [i]'部分找到搜索字符串,或者更具體地說,如果在最後一個找到該字符串,那麼您只返回true。 – pstrjds

回答

4

你有太多的關閉大括號...您縮進if在端困惑關閉} S的你的電話號碼:

 string fullText = container.ToString(); 
      if (!fullText.Contains(searchText)) 
      { 
       found = false; 
      } 
      else 
      { 
       found = true; 
      } 
     } // <<<< The indention of the if probably threw you off here... 
2

你的括號不匹配,所以返回的是函數外定義。

1

真的嗎?數你的打開和關閉括號。

2

這部分包含太多了},則終止該方法:

 string fullText = container.ToString(); 
       if (!fullText.Contains(searchText)) 
       { 
        found = false; 
       } 
       else 
       { 
        found = true; 
       } 
      } // remove this one 
2

您已經在方括號大括號之後放置了return found;。正確對齊代碼以查看。使用Ctrl-K,Ctrl-D在Visual Studio中格式化代碼。

相關問題