Find and display the position of a word in a sentence How can I simplify this code:

Public Class Form1
Private Sub BtnPositions_Click(sender As Object, e As EventArgs) Handles BtnPositions.Click
    ' 'txtSentence.Text = "The cat sat on the mat" ' Temp for testing

    If TxtAddSentence.Text = "" Then
        MsgBox("The text you are searching is empty, so there are no matches." &amp; vbCrLf &amp; "Please enter some text and retry.")
    End If

    Dim WordArray() = {""}
    Dim strSearchWord As String

    BuildWordArray(TxtAddSentence.Text, WordArray)
    strSearchWord = TxtAddWord.Text
    FindPositionOfWords(strSearchWord, WordArray)

    If TxtAddSentence.Text = "" Then
        MsgBox("The sentence box is empty, so there are no matches.")
    End If

End Sub
Private Sub BuildWordArray(ByVal strText, ByRef strArray)
    Dim strTextArray1() = Split(strText, " ")
    strArray = strTextArray1
End Sub

Private Sub FindPositionOfWords(ByVal strSearchWord, ByVal strWordList)

    Dim intIndex As Integer = 1
    Dim strPosn As String = ""
    For Each strWord As String In strWordList
        If UCase(strWord) = UCase(strSearchWord) Then
            If strPosn = "" Then
                strPosn = strPosn &amp; Str(intIndex)
            Else
                strPosn = strPosn &amp; "," &amp; Str(intIndex)
            End If
        End If
        intIndex = intIndex + 1
    Next
    If strPosn = "" Then
        MsgBox("The Search Word Here Box Is Empty", MsgBoxStyle.Exclamation)
    Else
        MsgBox("The word '" &amp; strSearchWord &amp; "' was found at positions" &amp; strPosn)
    End If
End Sub
End Class
please note I am not that great at vb.net

Variables - Define them! You haven't defined

   TxtAddWord
   TxtAddSentence (though I see txtSentence commented out)


are they defined on the form design?



Testing for something to be "" usually has a better way. I think something like this -

If String.IsNullOrEmpty(txtaddsentence) Then


Speaking of that if - you have it twice - why?

If TxtAddSentence.Text = "" Then
    MsgBox("The sentence box is empty, so there are no matches.")
End If


I can't see TxtAddSentence changing after the first check as there aren't any functions to return a string.



Speaking of strings, why are you always using

TxtAddSentence.Text


When you can just

    Dim TxtAddSentence = "The cat sat on the mat" ' Temp for testing
    Dim TxtAddWord = "sat"


Which are string type.



Naming - use variable names to describe what is happening, and avoid that hungarian notation.

Variable -&gt; can be -&gt;
TxtAddSentence      sentenceAddition
TxtAddWord          wordToAdd
strSearchWord       searchWord
strText             why? it's already sentenceAddition
strArray            why? it's already wordArray
strWordList         what is this? a List Type? An Array Type? A String?       
intIndex            index
strPosn             searchPosition
strWord             listedWord




Additionally, you're not giving a type to your variables:

ByVal strText, ByRef strArray)
ByVal strSearchWord, ByVal strWordList


Can be, as far as I can tell

Private Sub FindPositionOfWords(ByVal searchWord As String, ByVal wordArray() As String)
Private Sub BuildWordArray(ByVal sentenceAddition, ByRef wordArray)


It would be best practice to give a type to the variables in BuildWordArray - but I'm not entirely sure what type they should be.

Here -

Dim strPosn As String = ""


If this is position, why isn't it an integer? I would expect an integer, not a string.