4 Replies - 422 Views - Last Post: 04 December 2019 - 05:01 PM Rate Topic: -----

#1 Dreamfall   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 26-January 15

Reading and Writing sorted of a dictionary !

Posted 04 December 2019 - 03:34 PM

Hi guys, I can't understand what sorting logic is used to write and read a file in the default format, in the ResourceWriter and ResourceReader classes.
In the example I get a disorderly reading output in the enumerating the dictionary elements.

    Private Sub Button3_Click ....
        ' Create a file stream to encapsulate items.resources.
        Dim fs As New FileStream("items.resources", FileMode.Create, FileAccess.Write)

        ' set variables
        Dim writer = New Resources.ResourceWriter(fs)

        'write data to file
        writer.AddResource("1_Home", "My Test String of Text 1")
        writer.AddResource("2_Dog", "My Test String of Text 2")
        writer.AddResource("3_Cat", "My Test String of Text 3")
        writer.AddResource("4_Banana", "My Test String of Text 4")
        writer.AddResource("5_Armor", "My Test String of Text 5")

        ' Write the resources to the stream, and close it.
        writer.Close()

    End Sub



    Private Sub Button4_Click...
        Dim Resource As New Resources.ResourceReader("items.resources")
        Dim Dictionary As IDictionaryEnumerator = Resource.GetEnumerator()

        While (Dictionary.MoveNext())
            MsgBox(Dictionary.Key)
        End While

        Resource.Close()
    End Sub



In Output I get:

1) 1_Home
2) 4_Banana
3) 3_Cat
4) 2_Dog
5) 5_Armor

And not in order as I added the resources in the list like this:

1) 1_Home
2) 2_Dog
3) 3_Cat
4) 4_Banana
5) 5_Armor



I tried all the ways but I could not understand if it is possible to do it or how to order the reading in descending or ascending order.

I also tried this way, but it gives me the error "Enumeration already finished".

    Dim ReadList As String = String.Empty

    Private Sub Button4_Click...
        Dim Resource As New Resources.ResourceReader("items.resources")
        Dim Dictionary As IDictionaryEnumerator = Resource.GetEnumerator()

        While (Dictionary.MoveNext())
            ReadList += Dictionary.Key & " "
            MsgBox(Dictionary.Key)
        End While
        For Each Value As String In ReadList.Split(" ").OrderByDescending(Function(X) X.ToString)
            If Not Value.Length > 0 Then Exit For
            '//
            MsgBox(Value.ToString)
            If Value = "1_Home" Then
                MsgBox("1" & Value.ToString)
            End If
            If Value = "2_Dog" Then
                MsgBox("2" & Value.ToString)
            End If
            If Value = "3_Cat" Then
                MsgBox("3" & Value.ToString)
            End If
        Next

...


This post has been edited by Dreamfall: 04 December 2019 - 03:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading and Writing sorted of a dictionary !

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15477
  • View blog
  • Posts: 62,007
  • Joined: 12-June 08

Re: Reading and Writing sorted of a dictionary !

Posted 04 December 2019 - 03:48 PM

Any particular reason you are using that over 'stream reader/writer'?


Quote

The resources will not necessarily be written in the same order they were added.

https://docs.microso...etframework-4.8
Was This Post Helpful? 0
  • +
  • -

#3 Dreamfall   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 26-January 15

Re: Reading and Writing sorted of a dictionary !

Posted 04 December 2019 - 03:56 PM

View Postmodi123_1, on 04 December 2019 - 03:48 PM, said:

Any particular reason you are using that over 'stream reader/writer'?


Quote

The resources will not necessarily be written in the same order they were added.

https://docs.microso...etframework-4.8


Is an easy way to create and read in an external resource file (not using the resource panel in VS). so as to archive all the settings of the application and external files compressed in a file. Very fast to write in a code is very reliable.

Is there a way I don't know in order to sort and write resources in the same way they were added ??? :nottalkingtoyou:

This post has been edited by Dreamfall: 04 December 2019 - 04:03 PM

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15477
  • View blog
  • Posts: 62,007
  • Joined: 12-June 08

Re: Reading and Writing sorted of a dictionary !

Posted 04 December 2019 - 04:43 PM

Fair enough.. I typically use streamwriter just to have a consistent fashion, but many ways to skin a cat.

Per the docs, no. "The resources will not necessarily be written in the same order they were added.".
Was This Post Helpful? 0
  • +
  • -

#5 Dreamfall   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 26-January 15

Re: Reading and Writing sorted of a dictionary !

Posted 04 December 2019 - 05:01 PM

View Postmodi123_1, on 04 December 2019 - 04:43 PM, said:

but many ways to skin a cat.


:sarcasm: :eek:

OK Ok thanks ... even if it is true many times a sentence can contain so many answers .... the difficulty is knowing how to interpret it in the right way. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1