Results 1 to 5 of 5

Thread: [2005] Won't edit entry!!!

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    56

    [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?

  2. #2
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] Won't edit entry!!!

    vb.net Code:
    1. My.Computer.FileSystem.DeleteFile("FilePath")
    2.         My.Computer.FileSystem.WriteAllText("FilePath", counter & " : " & definition, True)
    3.         My.Computer.FileSystem.ReadAllText("FilePath")

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Won't edit entry!!!

    Why do this:
    vb.net Code:
    1. My.Computer.FileSystem.DeleteFile("FilePath")
    2. My.Computer.FileSystem.WriteAllText("FilePath", counter & " : " & definition, True)
    when you can just do this:
    vb.net Code:
    1. My.Computer.FileSystem.WriteAllText("FilePath", counter & " : " & definition, False)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    56

    Question 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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim definitionsByTerm As New Dictionary(Of String, String)
    2.  
    3. Using reader As New IO.StreamReader("file path here")
    4.     While Not reader.EndOfStream
    5.         'Add an item with the term line as the key and the definition line as the value.
    6.         definitionsByTerm.Add(reader.ReadLine(), reader.ReadLine())
    7.     End While
    8. End Using
    9.  
    10. Using writer As New IO.StreamWriter("file path here")
    11.     For Each item As KeyValuePair(Of String, String) In definitionsByTerm
    12.         MessageBox.Show(item.Value, item.Key)
    13.  
    14.         'Write the term to the file.
    15.         writer.WriteLine(item.Key)
    16.  
    17.         'Write the definition to the file.
    18.         writer.WriteLine(item.Value)
    19.     Next item
    20. 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:
    1. Dim someDefinition As String = definitionsByTerm(someTerm)
    2.  
    3. definitionsByTerm(someOtherTerm) = someOtherDefinition
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width