Recent Files List to File problem.
Recent Files List to File problem.
Recent Files List to File problem.
Recent Files List to File problem.
Recent Files List to File problem.
Recent Files List to File problem. Recent Files List to File problem. Recent Files List to File problem. Recent Files List to File problem. Recent Files List to File problem. Recent Files List to File problem. Recent Files List to File problem. Recent Files List to File problem.
Recent Files List to File problem. Recent Files List to File problem.
Recent Files List to File problem.
Go Back  Xtreme Visual Basic Talk > > > Recent Files List to File problem.


Reply
 
Thread Tools Display Modes
  #1  
Old 05-23-2012, 10:07 AM
SPV2008 SPV2008 is offline
Newcomer
 
Join Date: May 2012
Posts: 2
Default Recent Files List to File problem.


I'm currently using an array to store the latest 4 files that have been used. I am using the array recentfiles(3) to store the latest files, but i am only successfully writing one name to the right place.

Code:
Public Sub updaterecent()
        If Not recentfiles(3) = vbNullString Then
            recentfiles(2) = recentfiles(3)
            recentfiles(1) = recentfiles(2)
            recentfiles(0) = recentfiles(1)
            recentfiles(3) = SaveName

        ElseIf recentfiles(3) = vbNullString Then
            recentfiles(3) = SaveName

        ElseIf recentfiles(2) = vbNullString Then
            recentfiles(2) = SaveName

        ElseIf recentfiles(1) = vbNullString Then
            recentfiles(1) = SaveName

        ElseIf recentfiles(0) = vbNullString Then
            recentfiles(0) = SaveName

        End If

End Sub
SaveName is the name of the file name.

Any help would be appreciated. Thanks.
Reply With Quote
  #2  
Old 05-23-2012, 11:57 AM
snarfblam's Avatar
snarfblamRecent Files List to File problem. snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 896
Default

I'd suggest using a List(of String) instead. Arrays take more work, and are rarely worth the effort.

Code:
Dim recentfiles As New List(of String)
Public Sub updaterecent()
        
    ' Delete the oldest entry
    If recentfiles.Count > 3 Then
       recentfiles.RemoveAt(0)
    End If

    recentfiles.Add(SaveName)
End Sub
Reading the contents back out of a List object is exactly the same as with an array.
Code:
MessageBox.Show(recentfiles(0))
__________________
C# _VB.NET _
Reply With Quote
  #3  
Old 05-23-2012, 12:27 PM
passel's Avatar
passelRecent Files List to File problem. passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,028
Default

snarfblam's suggestion is simplest, and probably the best way to go.
But you do have a chance to debug some logic here, so for fun, before you switch to the better method, you might want to practice some debugging.
Have you set a break point in the code and stepped through it to see what the values are as you add each name to the "list".

Just looking at the logic of the code, shows there are cases that will never be hit.
Can you answer the questions:
Q1. Which index of the array will be the first used, index 0 or index 3?
Q2. After the first file name is added, what are the values of the four array elements?
Q3. After the second file name is added, what are the values of the four array elements? (big clue to the logic error here)
Q4. Will the last three ElseIf condition blocks ever be entered?
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #4  
Old 05-23-2012, 02:42 PM
SPV2008 SPV2008 is offline
Newcomer
 
Join Date: May 2012
Posts: 2
Default

Thanks for the feedback. yeah. I understand there's some logic errors in my code, just wasn't 100% sure what it was.

I have a problem with the declared item as string. It says it hasn't been declared as an object. This happens whenever the counting is happening.


Code:
Module Module1
    Public SaveName As String
    Public path(3) As String
    Public Save(3) As String
    Public recentfiles As List(Of String)
    Public recentfileslist() As String

    Public Sub PopulateRecentFiles()
        If My.Computer.FileSystem.FileExists("config.txt") Then
            Checklist.RecentFiles1.Text = recentfiles(3)

            If Not Checklist.RecentFiles2.Text = " " Then
                Checklist.RecentFiles2.Visible = True
                Checklist.RecentFiles2.Text = recentfiles(2)
            End If
            If Not Checklist.RecentFiles3.Text = " " Then
                Checklist.RecentFiles3.Visible = True
                Checklist.RecentFiles3.Text = recentfiles(1)
            End If
            If Not Checklist.RecentFiles4.Text = " " Then
                Checklist.RecentFiles4.Visible = True
                Checklist.RecentFiles4.Text = recentfiles(0)
            End If
        Else
            Exit Sub
        End If
    End Sub

    Public Sub ReadRecent()
        If My.Computer.FileSystem.FileExists("config.txt") Then

            Using objreader As IO.StreamReader = New IO.StreamReader("config.txt")
                Dim myText() As String
                While Not objreader.EndOfStream

                    recentfileslist = Split(objreader.ReadLine, ",")
                End While

                If recentfiles.Count >= 3 Then
                    recentfiles.Clear()
                End If

                recentfiles.Add(recentfileslist(0))
                recentfiles.Add(recentfileslist(1))
                recentfiles.Add(recentfileslist(2))
                recentfiles.Add(recentfileslist(3))

                objreader.Close()
                objreader.Dispose()
            End Using
        Else
            Exit Sub
        End If
    End Sub

    Public Sub Writerecent()
        Dim objWriter As New IO.StreamWriter("config.txt")

        objWriter.WriteLine(recentfiles(3) & "," & recentfiles(2) & "," & recentfiles(1) & "," & recentfiles(0))

        objWriter.Close()
    End Sub

    Public Sub updaterecent()
        ' Delete the oldest entry
        If recentfiles.Count > 3 Then
            recentfiles.RemoveAt(0)
        End If

        recentfiles.Add(SaveName)

    End Sub

End Module

Last edited by SPV2008; 05-23-2012 at 04:50 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Recent Files List to File problem.
Recent Files List to File problem.
Recent Files List to File problem. Recent Files List to File problem.
Recent Files List to File problem.
Recent Files List to File problem.
Recent Files List to File problem. Recent Files List to File problem. Recent Files List to File problem. Recent Files List to File problem. Recent Files List to File problem. Recent Files List to File problem. Recent Files List to File problem.
Recent Files List to File problem.
Recent Files List to File problem.
 
Recent Files List to File problem.
Recent Files List to File problem.
 
-->