-1

I'm using Settings feature of Visual Studio with string type and user scope.

I try to list last used recent files on my menu item but i think my approach not seems good for coding approach too much if else statement i used.

Is there any way to get it with better approach ?

 Public Sub addRecentUsedFiles(recentFile As String)
        If recentFile <> My.Settings.recent1 Then
            If recentFile <> My.Settings.recent2 Then
                If recentFile <> My.Settings.recent3 Then
                    If recentFile <> My.Settings.recent4 Then
                        My.Settings.recent5 = My.Settings.recent4
                        My.Settings.recent4 = My.Settings.recent3
                        My.Settings.recent3 = My.Settings.recent2
                        My.Settings.recent2 = My.Settings.recent1
                        My.Settings.recent1 = recentFile
                    Else
                        My.Settings.recent4 = My.Settings.recent3
                        My.Settings.recent3 = My.Settings.recent2
                        My.Settings.recent2 = My.Settings.recent1
                        My.Settings.recent1 = recentFile
                    End If
                Else
                    My.Settings.recent3 = My.Settings.recent2
                    My.Settings.recent2 = My.Settings.recent1
                    My.Settings.recent1 = recentFile
                End If
            Else
                My.Settings.recent2 = My.Settings.recent1
                My.Settings.recent1 = recentFile
            End If
        End If
  • 1
    See **[Method to “remember” past entries](https://stackoverflow.com/q/31121087/1070452)** perhaps. The thing you might want to do differently is to use a simple `List(Of T)` rather than BindingList. Then serialize it for saving rather than using Settings. Rather than any sort of strict limit, save them all and when you deseriazlize at app start up, trim it back to the past 5. – Ňɏssa Pøngjǣrdenlarp Apr 15 '18 at 21:22
  • 1
    User settings can store any serializable type. Instead of having five separate entries, use a collection type. [System.Collections.Specialized.StringCollection Class](https://msdn.microsoft.com/en-us/library/system.collections.specialized.stringcollection(v=vs.110).aspx) supports all the methods you should need. You can select this type from the "Type Dropdown" when defining the user setting. – TnTinMn Apr 15 '18 at 22:24

2 Answers2

1

I recommend a queue of fixed size for this approach. See the data structure defined here. I have it below but made a lot of changes to it. Fixed size queue which automatically dequeues old values upon new enques

In your case, you would the data type of String and set the limit to 5.

I've included the base code, removed the concurrency, and added a Contains method. Also added an enumerator so you can access the data.

    [Serializable()]
    public class FixedSizedQueue<T>
    {
        System.Collections.Generic.Queue<T> q = new System.Collections.Generic.Queue<T>();

        public int Limit { get; set; }
        public void Enqueue(T obj)
        {
            q.Enqueue(obj);
            while (q.Count > Limit) q.Dequeue() ;
        }

        public bool Contains(T obj)
        {
            return q.Contains(obj);
        }

        public Queue<T>.Enumerator GetEnumerator()
        {
            return q.GetEnumerator();
        }
    }

Then how you would use it, assuming My.Settings.Recent was of this type of String, you assigned the limit of 5 and the My.Settings.Recent was not null.

if ( ! My.Settings.Recent.Contains(recentFile) )
    My.Settings.Recent.Enqueue(recentFile);

Here's another example of writing data to the object, that only after the code has "6" to "10".

FixedSizedQueue<string> t = new FixedSizedQueue<string>();
t.Limit = 5;
for (int i = 0; i <= 10; i++)
    if (! t.Contains(i.ToString())) 
        t.Enqueue(i.ToString());
Ctznkane525
  • 7,297
  • 3
  • 16
  • 40
  • I'm currently not succeeded to apply this but i'm working on it. I'll give my feedbacks and accept as an answer. –  Apr 17 '18 at 07:12
  • ok...if you need anything let me know...the fact that the object is serializable i added due to it being placed in the my.settings – Ctznkane525 Apr 17 '18 at 11:59
0

Your easiest approach will be to write the Recently used files(file names) to a text file.Sample code :

  File.WriteAllText("C:\test.txt","last file name")

Now, if you use the WriteAllText method,it will rewrite(or create) the text file.If you want to write to the text file from different places,you can simply use File.AppendText method :

 File.AppendText("C:\test.txt","last file name" + Environment.NewLine)

Then, if you want to retrieve the file names from the text file,you can simply create a List(of String) which will read(and store data from) the text file :

  Dim readFile As New List(Of String)(File.ReadAllLines("C:\test.txt")

Now you have a list which has successfully stored each line from the text file as a separate item.You easily iterate through the list and do whatever you want with it,such as :

 For Each Item In readFile
  ComboBox1.Items.Add(Item)
 Next

Hope this helps to enrich your knowledge :)

Software Dev
  • 5,368
  • 5
  • 22
  • 45