2012-02-08 3 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

oscar chkwordは、パラメータワードがデータベースに存在するかどうかを調べる関数です.SQL Serverがダウンしていますが、値が存在する場合は1を返し、値が存在しない場合は0を返します。値が存在しない場合は0を返します。それが存在するというよりは、この単語が許可されていないと言ってメッセージを返します**** – Ironsun

+0

私は各単語の結果を得ることができるようにループを書く必要がありますか? – Ironsun

0

私は1つの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変数を完全に無視します。私は、各単語があなたの結果の1つを返すように辞書オブジェクトを返す関数で私の例を更新しました。 – LarsTech

関連する問題