2012-02-08 33 views
0

我試圖創建一個web方法來檢查一個字符串是否包含一個單詞,該列表中的'不允許'單詞。此列表將不時更新。從字符串中提取單詞並驗證它們是否存在於數據庫中的VB

<System.Web.Services.WebMethod()> _ 
Public Function checkword(ByVal Id As String) As String 
    Dim returnValue As String = String.Empty 
    Dim s As String = Id 

    ' Split string based on spaces 
    Dim words As String() = s.Split(New Char() {" "c}) 

    Dim rer As Integer 
    Dim word As String 
    For Each word In words 

    'call class file 
    Dim oscar As New webfunctions 

    Try 
     'call function to check whether this word exists in database or not, 
     rer = oscar.chkword(word) 
     If rer > 0 Then 
     returnValue = "a" 
     Else 
     returnValue = "exists" 
     End If 

    Catch ex As Exception 
     returnValue = ex.Message 
    End Try 
    Next 
    Return returnValue 
End Function 

此功能有些問題。它驗證字符串的第一個單詞。之後它不會返回任何東西。請有什麼建議嗎?

回答

1

你正在循環你的所有單詞,只返回最後一個單詞的結果。

從代碼中不太清楚它是如何工作的,因爲您返回「a」,「exists」或錯誤消息。

如果代碼找到一個「不允許的」單詞(哪一個顯然對您無關緊要),或者它引發了錯誤(這也不清楚它爲什麼會出現拋出一個錯誤):

For Each word In words 

    'call class file 
    Dim oscar As New webfunctions 

    Try 

    'call function to check whether this word exists in database or not, 
    rer = oscar.chkword(word) 
    If rer > 0 Then 
    returnValue = "a" 
    Else 
    return "exists" 
    End If 

    Catch ex As Exception 
    return ex.Message 
    End Try 
Next 
Return returnValue 

根據您的評論和更新的代碼,我可能會重新工作,你的函數返回的結果爲每個單詞的字典,因爲這似乎是你正在尋找:

<System.Web.Services.WebMethod()> _ 
Public Function CheckWords(ByVal sentence As String) As Dictionary(Of String, String) 
    Dim wordDictionary As New Dictionary(Of String, String) 

    For Each word As String In sentence.Split(" ") 
    Dim wordResult As String 
    Try 
     Dim oscar As New webfunctions 
     If oscar.chkword(word) > 0 Then 
     wordResult = "a" 
     Else 
     wordResult = "exists" 
     End If 
    Catch ex As Exception 
     wordResult = ex.Message 
    End Try 

    If Not wordDictionary.ContainsKey(word) Then 
     wordDictionary.Add(word, wordResult) 
    End If 
    Next 

    Return wordDictionary 
End Function 
+0

奧斯卡chkword是在存儲過程中,以檢查在數據庫中是否存在或不那麼嘗試參數字功能是櫃面的SQL服務器已關閉,但是當值存在,並且0則返回1,如果沒有這麼一個當值犯規存在而不是當它說存在時,它會返回一條消息,說這個詞是不允許的,並用****替換它 – Ironsun 2012-02-09 11:22:02

+0

我應該如何編寫循環,以便我可以得到每個單詞的結果? – Ironsun 2012-02-09 13:45:40

0

我使用了一個stringbuilder,現在它解決了

<System.Web.Services.WebMethod()> _ 
Public Function checkword(ByVal Id As String) As String 
    Dim returnValue As String = String.Empty 
    Dim s As String = Id 
dim sb as new stringBuilder 
    ' Split string based on spaces 
    Dim words As String() = s.Split(New Char() {" "c}) 

    Dim rer As Integer 
    Dim word As String 
    For Each word In words 

    'call class file 
    Dim oscar As New webfunctions 

    Try 
     'call function to check whether this word exists in database or not, 
     rer = oscar.chkword(word) 
     If rer > 0 Then 
     returnValue = "a" 
     Else 
     sb.append(word & ",") 
     End If 

    Catch ex As Exception 
     returnValue = ex.Message 
    End Try 
    Next 
    Return sb.tostring + "exists" 
End Function 
+0

您的重新工作只返回StringBuilder並完全忽略returnValue變量。我用一個返回字典對象的函數更新了我的示例,以便每個字都返回一個結果。 – LarsTech 2012-02-09 15:48:41

相關問題