|
-
May 27th, 2008, 10:13 AM
#1
Thread Starter
Member
[2005] Won't edit entry!!!
Hi all,
I'm trying to create a program... (Who isn't here -.-)
Well, I want to edit an entry in my current dictionary, but it just adds it.
This is my code:
Code:
Private Sub SaveData()
SR.Close()
Dim SW As StreamWriter
My.Computer.FileSystem.DeleteFile("Dictionary.txt")
SW = New StreamWriter("Dictionary.txt")
For Counter = 0 To NumTerms
SW.WriteLine(Term(Counter))
SW.WriteLine(Definition(Counter))
Next
SW.Close()
End Sub
Private Sub LoadDictionary()
NumTerms = -1
SR = New StreamReader("Dictionary.txt")
Do While SR.EndOfStream = False
NumTerms += 1
Term(NumTerms) = SR.ReadLine
Definition(NumTerms) = SR.ReadLine
Loop
End Sub
Private Sub btn_Save2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Save2.Click
Term(lst_results.SelectedIndex) = txt_Name.Text
Definition(lst_results.SelectedIndex) = txt_Description.Text
SaveData()
LoadDictionary()
End Sub
I want to delete the text file and then "re-write" it all back, then load it again. Anyone have any ideas?
-
May 27th, 2008, 06:41 PM
#2
Fanatic Member
Re: [2005] Won't edit entry!!!
vb.net Code:
My.Computer.FileSystem.DeleteFile("FilePath") My.Computer.FileSystem.WriteAllText("FilePath", counter & " : " & definition, True) My.Computer.FileSystem.ReadAllText("FilePath")
-
May 27th, 2008, 06:55 PM
#3
Re: [2005] Won't edit entry!!!
Why do this:
vb.net Code:
My.Computer.FileSystem.DeleteFile("FilePath") My.Computer.FileSystem.WriteAllText("FilePath", counter & " : " & definition, True)
when you can just do this:
vb.net Code:
My.Computer.FileSystem.WriteAllText("FilePath", counter & " : " & definition, False)
-
May 29th, 2008, 04:43 AM
#4
Thread Starter
Member
Re: [2005] Won't edit entry!!!
Okay, I've tried both, and I can't get it to work.
I've got two arrays:
Term()
and
Definition()
When the dictionary loads up, it reads all the terms and definitions to those arrays using the following code:
Code:
Private Sub LoadDictionary()
NumTerms = -1
SR = New StreamReader("Dictionary.txt")
Do While SR.EndOfStream = False
NumTerms += 1
Term(NumTerms) = SR.ReadLine
Definition(NumTerms) = SR.ReadLine
Loop
End Sub
That works perfectly, however, when I go to edit an entry, you want to edit it and the editing take effect straight away usually wouldn't you?
Well, that's what I can't do...
Once the user edits an entry, I want the dictionary to re-write ALL the terms and definitions to a text file and then re-load the dictionary.
It will have to write each term the it's definition, so I think the code would look something like this:
Code:
Private Sub SaveData()
SR.Close()
My.Computer.FileSystem.DeleteFile("Dictionary.txt")
For Counter = 0 To NumTerms
My.Computer.FileSystem.WriteAllText("Dictionary.txt", Term(Counter), True)
My.Computer.FileSystem.WriteAllText("Dictionary.txt", Definition(Counter), True)
Next
End Sub
But it doesn't work ... Any ideas here?
Thanks in advance.
-
May 29th, 2008, 08:40 AM
#5
Re: [2005] Won't edit entry!!!
Do you know what WriteAllText does? It opens a file, writes all the text you specify, then closes the file. Do you really want to open and close the same file twice for every entry you write to the database?
You should be writing it as you're reading it. If you're using StreamReader.ReadLine to read lines alternately from the file then it would make sense to use StreamWriter.WriteLine to write lines alternately to the file.
That said, you shouldn't be using two separate lists for the terms and definitions and you don't need that NumTerms variable either:
vb.net Code:
Dim definitionsByTerm As New Dictionary(Of String, String) Using reader As New IO.StreamReader("file path here") While Not reader.EndOfStream 'Add an item with the term line as the key and the definition line as the value. definitionsByTerm.Add(reader.ReadLine(), reader.ReadLine()) End While End Using Using writer As New IO.StreamWriter("file path here") For Each item As KeyValuePair(Of String, String) In definitionsByTerm MessageBox.Show(item.Value, item.Key) 'Write the term to the file. writer.WriteLine(item.Key) 'Write the definition to the file. writer.WriteLine(item.Value) Next item End Using
If you want to get or set a definition in the collection in between you just index the Dictionary by key, which means with a term, e.g.
vb.net Code:
Dim someDefinition As String = definitionsByTerm(someTerm) definitionsByTerm(someOtherTerm) = someOtherDefinition
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|