Basically, for my summer work for school, I have to create a program that will calculate the Flesch Index of a text document. I am required to store each unique word in a collection. Collections are brand new to me at this point, and I've had to figure everything out on my own! I also had to teach myself how to make my own objects. So here's what I've got.
I've made my "word" class so I can use objects of "word" type.
Public Class Word Private strName As String Private blnPalindrome As Boolean Private intLength As Integer Private intQnty As Integer Private intSyllables As Integer Private intVowels As Integer Private intConsonants As Integer Public Property Name() As String Get Return strName End Get Set(ByVal value As String) strName = value End Set End Property Public Property Palindrome() As Boolean Get Return blnPalindrome End Get Set(ByVal value As Boolean) blnPalindrome = value End Set End Property Public Property Length() As Integer Get Return intLength End Get Set(ByVal value As Integer) intLength = value End Set End Property Public Property Qnty() As Integer Get Return intQnty End Get Set(ByVal value As Integer) intQnty = value End Set End Property Public Property Syllables() As Integer Get Return intSyllables End Get Set(ByVal value As Integer) intSyllables = value End Set End Property Public Property Vowels() As Integer Get Return intVowels End Get Set(ByVal value As Integer) intVowels = value End Set End Property Public Property Consonants() As Integer Get Return intConsonants End Get Set(ByVal value As Integer) intConsonants = value End Set End Property Public Function GetVowel() Dim strNameCopy As String strNameCopy = strName intVowels = 0 Do Until Len(strNameCopy) = 0 Select Case strNameCopy.Substring(0, 1) Case "a", "e", "i", "o", "u", "y" intVowels = intVowels + 1 strNameCopy = strNameCopy.Remove(0, 1) Case Else strNameCopy = strNameCopy.Remove(0, 1) End Select Loop Return intVowels Return Vowels End Function Public Function GetConsonant() intConsonants = Len(strName) - intVowels Return intConsonants Return Consonants End Function Public Function GetLength() intLength = Len(strName) Return intLength End Function End Class
The code isn't complete yet, but i'm working on it. Also, if there's a way to make it more efficient, please tell me =)
So, I have all that, here's where things are going wrong:
Private Sub Populate_Collection()
Dim i As Integer
For i = 0 To UBound(strWords)
strWholeText = strWholeText & Trim(strWords(i)) & " "
Next
Do While Len(strWholeText) > 0
strWholeText = strWholeText.Trim()
strWholeText = strWholeText & " "
iPosit = strWholeText.IndexOf(" ")
strWordCheck = strWholeText.Substring(0, iPosit) 'Get the word
'Remove Punctuation
iLen = Len(strWordCheck)
Select Case strWordCheck.Substring(iLen - 1, 1)
Case ".", ",", "!", "?", ";", ":"
strWordCheck = strWordCheck.Remove(iLen - 1, 1)
Case Else
Exit Select
End Select
objWord.Name = strWordCheck
objWord.Vowels = objWord.GetVowel
objWord.Consonants = objWord.GetConsonant
Try
objWord.Qnty = 1
WordColl.Add(objWord, strWordCheck) 'Add word to collection, using its name as the "key"
Catch ex As Exception
objWord.Qnty = objWord.Qnty + 1
WordColl.Remove(strWordCheck) 'Remove it before adding it again
WordColl.Add(objWord, strWordCheck) 'Add word to collection, using its name as the "key"
End Try
'lstWordList.DataSource = WordColl
strWholeText = strWholeText.Remove(0, iLen)
strWholeText = Trim(strWholeText)
If Len(strWholeText) = 0 Then
Exit Do
End If
Loop
For Each Word As Object In WordColl
objTemp = Word
lstWordList.Items.Add(objTemp.Name)
Next
End Sub
That is a Sub in my main Form1 Class.
so, what's going wrong is that when I use:
For Each Word As Object In WordColl objTemp = Word lstWordList.Items.Add(objTemp.Name) Next
to populate a list box, It turns out all the names in the collection are the same (Which is the last word from the file I opened.)
Is there something obviously, blatantly wrong here that I'm stupidly missing? And could someone point it out? Or do I have the entire Idea of collections wrong?
Thanks
Erik

New Topic/Question
Reply




MultiQuote





|