Wednesday, May 11, 2011

VB Practice 6 - Methods


Public Class Form1
Dim chara, word, sentVal, space As Integer
Private Sub btnAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click
'nchar = GetCharCount(TextBox1.Text)
chara = TextBox1.Text.Length.ToString
word = getWordCount(TextBox1.Text)
space = getSpacesCount(TextBox1.Text)
sentVal = getSentenceCount(TextBox1.Text)
updatestats()
End Sub
Private Function getWordCount(ByVal value As String) As Integer
Dim word As String
word = System.Text.RegularExpressions.Regex.Matches(value, "\S+").Count
Return word
End Function
Private Function getSentenceCount(ByVal value As String) As Integer
Dim sentVal As String = 1
sentVal = System.Text.RegularExpressions.Regex.Matches(value, "\.").Count
sentVal += System.Text.RegularExpressions.Regex.Matches(value, "\!").Count
sentVal += System.Text.RegularExpressions.Regex.Matches(value, "\?").Count
Return sentVal
End Function
Private Function getSpacesCount(ByVal value As String) As Integer
For Each s As String In value
If s = " " Then
space += 1
End If
Next
Return space
End Function
Private Sub updatestats()
txtChara.Text = chara
txtword.Text = word
txtspaces.Text = space
txtSent.Text = sentVal
End Sub

End Class

No comments:

Post a Comment