R
ryguy7272
I am trying to run the below code, which I found in a book named Mastering
Visual basic.NET:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.DefaultExt = "TXT"
OpenFileDialog1.Filter = "Text|*.TXT|All Files|*.*"
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName = "" Then Exit Sub
Dim str As StreamReader
Dim txtFile As File
Dim txtLine As String
Dim Words() As String
Dim Delimiters() As Char = {CType(" ", Char), CType(".", Char), _
CType(",", Char), CType("‘", Char), _
CType("!", Char), CType(";", Char), _
CType(":", Char), Chr(10), Chr(13)}
str = File.OpenText(OpenFileDialog1.FileName)
txtLine = str.ReadLine()
txtLine = str.ReadToEnd
Words = txtLine.Split(Delimiters)
Dim iword As Integer, word As String
For iword = 0 To Words.GetUpperBound(0)
word = Words(iword).ToUpper
If IsValidWord(word) Then
If Not WordFrequencies.ContainsKey(word) Then
WordFrequencies.Add(word, 1)
Else
WordFrequencies(word) = CType(WordFrequencies(word),
Integer) + 1
End If
End If
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim wEnum As IDictionaryEnumerator
Dim occurrences As Integer
Dim allWords As New System.Text.StringBuilder()
wEnum = WordFrequencies.GetEnumerator
While wEnum.MoveNext
allWords.Append(wEnum.Key.ToString & vbTab & "—>" & vbTab & _
wEnum.Value.ToString & vbCrLf)
End While
TextBox1.Text = allWords.ToString
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
Dim wEnum As IDictionaryEnumerator
Dim Words(WordFrequencies.Count) As String
Dim Frequencies(WordFrequencies.Count) As Double
Dim allWords As New System.Text.StringBuilder()
Dim i, totCount As Integer
wEnum = WordFrequencies.GetEnumerator
While wEnum.MoveNext
Words(i) = CType(wEnum.Key, String)
Frequencies(i) = CType(wEnum.Value, Integer)
totCount = totCount + Frequencies(i)
i = i + 1
End While
For i = 0 To Words.GetUpperBound(0)
Frequencies(i) = Frequencies(i) / totCount
Next
Words.Sort(Frequencies, Words)
TextBox1.Clear()
For i = Words.GetUpperBound(0) To 0 Step -1
allWords.Append(Words(i) & vbTab & "—>" & vbTab & _
Format(100 * Frequencies(i), "#.000") & vbCrLf)
Next
TextBox1.Text = allWords.ToString
End Sub
End Class
I had a few squiggle lines, so I added this:
Imports System.IO
Some of the squiggles disappeared, but I still have squiggles under the
following: OpenFileDialog1, IsValidWord, WordFrequencies, and Words.Sort
I tried to run the project, and of course it didn’t work. My question is
how do I fix this? Also, and more importantly, how do I figure out what to
do the next time something like this happens? Is there a library or class
that needs to be imported? Is there a reference that needs to be added?
I’ve read a couple books on VB and encounter these types of things from time
to time, but I don’t really know how to resolve the issues myself, I guess
because I don’t have the level of experience of others. So I come to this
discussion group and get help sometimes, which is great!! However, I really
want to be able to resolve these issues by myself.
Thanks!
Ryan---
Visual basic.NET:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.DefaultExt = "TXT"
OpenFileDialog1.Filter = "Text|*.TXT|All Files|*.*"
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName = "" Then Exit Sub
Dim str As StreamReader
Dim txtFile As File
Dim txtLine As String
Dim Words() As String
Dim Delimiters() As Char = {CType(" ", Char), CType(".", Char), _
CType(",", Char), CType("‘", Char), _
CType("!", Char), CType(";", Char), _
CType(":", Char), Chr(10), Chr(13)}
str = File.OpenText(OpenFileDialog1.FileName)
txtLine = str.ReadLine()
txtLine = str.ReadToEnd
Words = txtLine.Split(Delimiters)
Dim iword As Integer, word As String
For iword = 0 To Words.GetUpperBound(0)
word = Words(iword).ToUpper
If IsValidWord(word) Then
If Not WordFrequencies.ContainsKey(word) Then
WordFrequencies.Add(word, 1)
Else
WordFrequencies(word) = CType(WordFrequencies(word),
Integer) + 1
End If
End If
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim wEnum As IDictionaryEnumerator
Dim occurrences As Integer
Dim allWords As New System.Text.StringBuilder()
wEnum = WordFrequencies.GetEnumerator
While wEnum.MoveNext
allWords.Append(wEnum.Key.ToString & vbTab & "—>" & vbTab & _
wEnum.Value.ToString & vbCrLf)
End While
TextBox1.Text = allWords.ToString
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
Dim wEnum As IDictionaryEnumerator
Dim Words(WordFrequencies.Count) As String
Dim Frequencies(WordFrequencies.Count) As Double
Dim allWords As New System.Text.StringBuilder()
Dim i, totCount As Integer
wEnum = WordFrequencies.GetEnumerator
While wEnum.MoveNext
Words(i) = CType(wEnum.Key, String)
Frequencies(i) = CType(wEnum.Value, Integer)
totCount = totCount + Frequencies(i)
i = i + 1
End While
For i = 0 To Words.GetUpperBound(0)
Frequencies(i) = Frequencies(i) / totCount
Next
Words.Sort(Frequencies, Words)
TextBox1.Clear()
For i = Words.GetUpperBound(0) To 0 Step -1
allWords.Append(Words(i) & vbTab & "—>" & vbTab & _
Format(100 * Frequencies(i), "#.000") & vbCrLf)
Next
TextBox1.Text = allWords.ToString
End Sub
End Class
I had a few squiggle lines, so I added this:
Imports System.IO
Some of the squiggles disappeared, but I still have squiggles under the
following: OpenFileDialog1, IsValidWord, WordFrequencies, and Words.Sort
I tried to run the project, and of course it didn’t work. My question is
how do I fix this? Also, and more importantly, how do I figure out what to
do the next time something like this happens? Is there a library or class
that needs to be imported? Is there a reference that needs to be added?
I’ve read a couple books on VB and encounter these types of things from time
to time, but I don’t really know how to resolve the issues myself, I guess
because I don’t have the level of experience of others. So I come to this
discussion group and get help sometimes, which is great!! However, I really
want to be able to resolve these issues by myself.
Thanks!
Ryan---