Question Problems with exercise

gieterke

New member
Joined
Nov 4, 2009
Messages
2
Programming Experience
Beginner
First of all I have to say that my Englisch is not very good. I can understand the most but I can't write Englisch very good. So my problem: I'm learning VB.NET and I'm doing an exercise over text files. My task is:
This contains Texting English followed by it’s Real
English translation. For example:
LOL = Laughing out loud
Each line uses an equals sign to separate the Texting English from it’s Real English
translation. When a button is clicked on your form, you job is to write the code that
opens this “dictionary” text file and read its contents. Once the text file is opened, write
code to place the Texting English in one List Box and the Real English in a second List
Box.
That’s clearly not enough, though. When a user clicks on a term in a List Box, display
the translation in a Textbox. So if LOL was selected from your List Box, the translation
“Laughing out loud” should appear in the textbox. But if “Laughing out loud” was
selected, the Texting version, LOL, should appear in the textbox.
Part Two
Add two new textboxes and a button. Write code so that the user can add a new
Text/Real English pair to the dictionary.

The code that I have got is this (I have write my problems in the comment.):

VB.NET:
                             Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLezen.Click
        Dim dictionary As String = "C:\Documents and Settings\Eigenaar\Bureaublad\dictionary.txt"
        Dim objReader As New System.IO.StreamReader(Dictionary)
        Dim arry() As String
        Dim text As String = ""


        Do Until objReader.Peek() = -1
            text = objReader.ReadLine
            text = text.Trim
            arry = text.Split("=")
            ListBox2.Items.Add(arry(1)) 'I got the following error: Index was outside the bounds of the array.
            ListBox1.Items.Add(arry(0))
        Loop

        objReader.Close()

    End Sub

    Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
        Dim x As Integer
        x = ListBox1.SelectedIndex
        txtOmgekeerde.Text = ListBox2.Items.Item(x)
    End Sub

    Private Sub ListBox2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox2.Click
        Dim x As Integer
        x = ListBox2.SelectedIndex
        txtOmgekeerde.Text = ListBox1.Items.Item(x)
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles schrijven.Click

        Dim dictionary As String = "C:\Documents and Settings\Eigenaar\Bureaublad\dictionary.txt"
        Dim objWriter As New System.IO.StreamWriter(dictionary, True)
        Dim arry(1) As String
        Dim text As String

        arry(0) = TextBox1.Text.Trim
        arry(1) = TextBox2.Text.Trim
        text = text.Join("=", arry) 'I got the following error on the part next to = :Warning 1 Access of shared member, constant member, enum member  						'or nested type through an instance; qualifying expression will not be evaluated.

        objWriter.WriteLine(text)
        objWriter.Close()
        MsgBox("gelukt")
        'it doesn't write to the text file.
    End Sub
End Class
Can anyone help me please?
Thanks
 
Replace
VB.NET:
text = text.Join("=", arry)
with
VB.NET:
text = String.Join("=", arry)
then text will have a value to write.

FYI, it is better to name variables that don't match other property names. Like myText instead of text.
 
Something else to think about. Using/End Using for Streamwriter/readers.
VB.NET:
Using objReader As New System.IO.StreamReader(Dictionary)
...
End Using
Or use objReader.Dispose. Using/End Using will dispose it for you.
 
One other possible problem everything in VB.Net is case sensitive so this might be a problem too:
VB.NET:
Dim [COLOR="Red"]dictionary[/COLOR] As String = "C:\Documents and Settings\Eigenaar\Bureaublad\dictionary.txt"
Dim objReader As New System.IO.StreamReader([COLOR="red"]Dictionary[/COLOR])
There is a Dictionary Class - Dictionary(Of TKey, TValue) in generics. This is why I mentioned your naming practice. Your array has no string items in it so of course it is out of bounds.
 
Back
Top