+ 1
Can you store a non-generic SortedList in a string, save it to a file, and upon the next start, load it from that file?
If you were to use the following code: using System.IO; using System.Collections; .... SortedList sl = new SortedList(); sl.Add("a", 0); sl.Add("b", "B"); sl.Add("c", true); string slString = sl.ToString(); File.WriteAllText(slString, path); //Pretend like the code below would execute on the next start, along with the SortedList definition and the using directives sl = File.ReadAllText(path); would it actually work? If not, what are other possibilities and what are the problems? I want to store options in a game in a non-generic SortedList, since there seem to be no problems when adding different types of values. Answers would be greatly appreciated.
15 ответов
+ 3
It sounds like what you want is serialization. This will allow you to store objects in binary form in a file and reload the data from that saved state.
https://www.guru99.com/c-sharp-serialization.html
https://www.c-sharpcorner.com/article/serializing-objects-in-C-Sharp/
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/serialization/
+ 3
Martin Taylor ChaoticDawg
I think I have found the solution: I think I have to store my SortedList like this: "value type, key, value"
Then I have to check what type the value is and I have to load it differently according to that.
+ 2
Martin Taylor
In my case, the SortedList is non-generic, aka not generic, and according to https://docs.microsoft.com/en-us/dotnet/api/system.collections.sortedlist?view=net-5.0 I wrote the definition correctly (SortedList sl = new SortedList();)
Anyways I'm sorry for bothering you if I bothered you
+ 2
ChaoticDawg unfortunately the problem I'm facing is that I want a non-generic SortedList to store values of many different types, as I want to get a settings menu for my project where I can effieciently, quickly and easily change and store some settings (values).
As I said it's uncertain for me whether this works or not until I build my project in Unity, as the file does not get created by the code and I don't know the actual structure in which a SortedList could/would be stored. The file does not get created because of an error that reads something along the lines of "Path could not be found"
Anyways thank you for your time and answers
+ 1
Martin Taylor
I don't know if I missed something but I only declared a SortedList variable in Visual Studio and even tried to not specify any value types. Then I used the sl.Add(key, value) function to add keys and values, with each value being of a different type and surprisingly I didn't get any error at all.
My code seems to be working so far but I wanted to know whether it would work after building my project in Unity because I can't test file functions without actually building my project in Unity and Unity is also constantly throwing the "File path (insert file path here) does not exist" error at me but I am able to press play and test the rest of the code without problems. So I assumed that I had to create the file but I don't know the data structure in which a SortedList would be stored if it were possible at all.
+ 1
C4Oc
True the (non-generic) SortedList doesn't support serialization, but if you convert its type into arrays (or another serializable type) you may be able to work around the issue. Try making a keys array and a values array from the SortedList and serialize them. Then you can reload those arrays and merge them back to the SortedList.
+ 1
Martin Taylor Thank you for clarifying that for me. I will try to find another way once I have time.
0
Martin Taylor
But how would I do it if I had multiple value types in the same SortedList as values?
0
Martin Taylor would my method still work or do I have to do it that way?
Thank you for your time and answers