Issues with Collections

New to collections. Something wrong with Keys?

Page 1 of 1

4 Replies - 647 Views - Last Post: 19 August 2009 - 01:07 PM Rate Topic: -----

#1 Erik_Cant_Code  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-August 09

Issues with Collections

Posted 18 August 2009 - 04:00 PM

Hi! I'm new here, and this is my first post. I'm having some issues with my code

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

Is This A Good Question/Topic? 0
  • +

Replies To: Issues with Collections

#2 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Issues with Collections

Posted 18 August 2009 - 05:27 PM

Several things to look at:

1 - For the punctuation removal, look at using the String.Split function on the line with all the punctuation and tab and space characters as split points. The elements of the resulting array will contain only valid words or empty strings - all the splitting characters are removed.

2 - For parsing the words, use String.ToLower to make all the words lowercase for consistent comparison.

3 - For walking the word to count vowels, you can use String.ToCharArray() to make the word an array of characters to make it easier to walk when looking for vowels.

4 - Rename your Subs as verbs - action words - where appropriate - e.g. CountVowels not GetVowels - you are not returning any vowels!

Will look at Collections on the next iteration.
Was This Post Helpful? 1
  • +
  • -

#3 Erik_Cant_Code  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-August 09

Re: Issues with Collections

Posted 19 August 2009 - 10:57 AM

Thanks for the suggestions Mark! That really helped clean up my code!

Still getting the same object repeated in the code.
Here are all my classes:

Word.vb
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 CountVowels()
		Dim charWord As Char()
		Dim strNameCopy As String

		strNameCopy = strName
		charWord = strNameCopy.ToCharArray

		intVowels = 0
		For i As Integer = 0 To UBound(charWord)
			Select Case charWord(i)
				Case "a", "e", "i", "o", "u", "y"
					intVowels = intVowels + 1
			End Select
		Next

		Return intVowels
		Return Vowels

	End Function

	Public Function CountConsonants()

		intConsonants = Len(strName) - intVowels
		Return intConsonants
		Return Consonants

	End Function

	Public Function FindLength()

		intLength = Len(strName)
		Return intLength

	End Function

End Class



Form1.vb
Public Class Form1

	Public objWord As New Word
	Dim objTemp As New Word
	Dim iPosit, iLen, iTemp As Integer
	Dim strWordCheck, strWholeText As String

	Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click

		Open_File()
		Populate_Collection()
		FileClose(1)

	End Sub

	'Opens file. File is Number 1, as usual!
	Private Sub Open_File()

		'On Error GoTo ErrorHandler

		Dim File As String
		Dim openFileDialog1 As New OpenFileDialog()
		Dim iMax, iCount, i As Integer

		' Sets the Dialog Title to Open File
		openFileDialog1.Title = "Open File"

		' Sets the File List box to text documents
		openFileDialog1.Filter = "Text Documents (*.txt)|*.txt"

		' Set the default files type to text documents
		openFileDialog1.FilterIndex = 1

		openFileDialog1.RestoreDirectory = True

		openFileDialog1.ShowDialog()
		File = openFileDialog1.FileName

		Try
			FileOpen(1, File, OpenMode.Input)
		Catch ex As Exception
			Exit Sub
		End Try


		'==The following dasplayes the file...

		txtDocDisp.Text = ""

		iCount = 0

		Do Until EOF(1)					 'keep going until end of file (EOF)

			iMax = UBound(strLines)		 'set iMax to the upper limit of array
			If iMax < iCount Then		   'If too many lines to be inserted...
				ReDim Preserve strLines(UBound(strLines) + 1) '... make array bigger
			End If

			strLines(iCount) = LineInput(1) 'Trim iCount line in file and add to array
			iCount = iCount + 1
		Loop

		For i = 0 To UBound(strLines)
			txtDocDisp.Text = txtDocDisp.Text & strLines(i) & vbCrLf
		Next

		'==End Of Display

		Exit Sub

		'ErrorHandler:  'handle them errors

		'		Select Case Err.Number
		'			Case 53 'Can't find file? 
		'				MsgBox("File does not exist", MsgBoxStyle.Critical, "Error!")
		'				Me.Close() 'msgbox discribing error and close program
		'			Case Else 'Other error?
		'				MsgBox("Error #" & Err.Number & "; " & Err.Description, MsgBoxStyle.Critical, "Fatal Error!")
		'				Me.Close() 'msgbox discribing error and close program
		'		End Select

		'		Resume Next 'go back to rest of sub
	End Sub

	Private Sub Populate_Collection()

		Dim i As Integer

		For i = 0 To UBound(strLines)
			strWholeText = strWholeText & Trim(strLines(i)) & " "
		Next

		strWholeText = strWholeText.ToLower

		strWords = strWholeText.Split(" ", ",", ".", ";", ":", "?", "!")

		Array.Sort(strWords)

		For i = 0 To UBound(strWords)
			If Not strWords(i) = "" Then
				objWord.Name = strWords(i)
				objWord.Vowels = objWord.CountVowels
				objWord.Consonants = objWord.CountConsonants

				Try
					objWord.Qnty = 1
					WordColl.Add(objWord, strWords(i)) 'Add word to collection, using its name as the "key"
				Catch ex As Exception
					objWord.Qnty = objWord.Qnty + 1
					WordColl.Remove(strWords(i)) 'Remove it before adding it again
					WordColl.Add(objWord, strWords(i)) 'Add word to collection, using its name as the "key"
				End Try
			End If
		Next

		For Each Word As Object In WordColl

			objTemp = Word

			lstWordList.Items.Add(objTemp.Name)

		Next

	End Sub

	Private Sub lstWordList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstWordList.SelectedIndexChanged

		objTemp = WordColl.Item(lstWordList.SelectedItem)
		txtWord.Text = objTemp.Name
		txtQnty.Text = objTemp.Qnty
		txtConsonant.Text = objTemp.Consonants
		txtVowel.Text = objTemp.Vowels

	End Sub

	Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
		Me.Close()
	End Sub

	Private Sub FleschIndexToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FleschIndexToolStripMenuItem.Click

		If FleschIndexToolStripMenuItem.Checked = True Then
			grpFleschIndex.Visible = True
		ElseIf FleschIndexToolStripMenuItem.Checked = False Then
			grpFleschIndex.Visible = False
		End If

	End Sub

	Private Sub WordInformationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WordInformationToolStripMenuItem.Click

		If WordInformationToolStripMenuItem.Checked = True Then
			grpWordInfo.Visible = True
		ElseIf WordInformationToolStripMenuItem.Checked = False Then
			grpWordInfo.Visible = False
		End If

	End Sub
End Class



and module1.vb (not really a class. just contains some global variables)
Module Module1

	Public intWordCount As Integer
	Public strLines(1) As String
	Public strText(0) As String
	Public strWords As String() = Nothing
	' Declare and create the Collection object.
	Public WordColl As New Microsoft.VisualBasic.Collection

End Module


Basically what happens is if the last word I add to the collection is "Years" then all the collection objects will be listed as having name = years, qnty = 1, consonants = 2 vowels = 3 etc. However, when doing a step-through the program, they all have the proper keys.

I have a feeling that I am doing something wrong with the objects. However, I'm very new to all this, and I'm going to need it pointed out. :(

Attached File(s)


This post has been edited by Erik_Cant_Code: 19 August 2009 - 10:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Issues with Collections

Posted 19 August 2009 - 11:18 AM

Try changing
	Public WordColl As New Microsoft.VisualBasic.Collection


to
Public  WordColl As New Dictionary(of String,Word)



So now check if a word is already in collection use
If WordCol.ContainsKey(<Word to look for>)
 Dim tmpWord As Word=WordCol(<Word To Look For>)
 ' modify / update
 WordCol(<Word To look for>)=tmpWord
End If


To add a new one
Dim newWord As New Word()
' do stuff
WordColl.Add( <Word String> , newWord )


Was This Post Helpful? 1
  • +
  • -

#5 Erik_Cant_Code  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-August 09

Re: Issues with Collections

Posted 19 August 2009 - 01:07 PM

Alright! Looks like that solved it! Thanks so much for your help and patience with my ignorance!

We'll see if the rest of the project goes more smoothly...

Thanks again!

Erik
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1