0

I need to remember certain integer array after program close and then to load on startup for the saving I done this:

List<int[]> lst = drops_counter.OfType<int[]>().ToList();
ArrayList arrayList = new ArrayList(lst);
Properties.Settings.Default.lstl = arrayList;

where lstl is from type ArrayList which is the only way to store an array in c# as I learned here. (if there is another type that will solve my problem dont mind to change)

For loading the array n startup I have done this

int[] f1 = Properties.Settings.Default.lstl.OfType<int>().ToArray();

But now I am stuck as I dont know how to convert object to array. if there is a solution for my problem or another way to solve this I will be more then happy because the only other way to do it is with EXCEL FILE and that means to depend on another program. Please help Sincerely yours Gildin Ilia

Jakub Jankowski
  • 731
  • 2
  • 9
  • 20
  • Where did you learn the "only way to store an array"? That's not true at all. – Enigmativity Oct 13 '16 at 12:00
  • 2
    Quick and dirty would to just serialize to a file, and deserialize the file on startup. Look into what serialization means, then it's all easy peasy. – Glubus Oct 13 '16 at 12:01
  • Your types seem wrong. DO you have a `List` or a `int[]`? – Enigmativity Oct 13 '16 at 12:01
  • List is a valid type is it not? – Glubus Oct 13 '16 at 12:01
  • @glubus the types used don't match – Oliver Oct 13 '16 at 12:02
  • 1
    You can store array in whatever persistent storage you can imagine: Properties file, any other text file, XML, Excel, CSV, Database, System registry, etc. You just need to find appropriate class which handles that file type. – Sasha Oct 13 '16 at 12:03
  • Try this: `File.WriteAllLines(@"file.txt", data.Select(x => x.ToString()));` and then this: `int[] restored = File.ReadLines(@"file.text").Select(x => int.Parse(x)).ToArray();`. – Enigmativity Oct 13 '16 at 12:03
  • One step forward to @Glubus comments. Here is how you can https://msdn.microsoft.com/en-us/library/bb412179(v=vs.110).aspx – Adnan Umer Oct 13 '16 at 12:03
  • http://stackoverflow.com/questions/1766610/how-to-store-int-array-in-application-settings – user1725145 Oct 13 '16 at 14:18

1 Answers1

0

You can save the ArrayList using Properties.Settings. A very good example can be found here how to use Settigns http://www.codeproject.com/Articles/17659/How-To-Use-the-Settings-Class-in-C

from the article:

If the scope is User then it can be changed at runtime and the new value will be stored for you - which you can then read another time the program is run. This is exactly what most of us need from this class,

  • But it is not what I need I already know how to save all the variables except arrays in fact I can save array but I have a problem load it back – ilia gildin Oct 13 '16 at 12:29
  • you might want to read it like this. Suppose you saved that value in DefaultAccount properties. if you have a variable name say account where you want to load data when you start your program, you can read it like => var account = .Properties.Settings.Default.Account; Are you using Settings as I suggested? – Md. Tazbir Ur Rahman Bhuiyan Oct 13 '16 at 13:21
  • 1
    yes it works fine with variables thanks for all the early answers helped me a lot I just now have the problem with array thats it I tried to save it the same way as variable it didnt work my boss on my first ever job told me to think more if I wont get answer until Sunday simply save in EXCEL file – ilia gildin Oct 13 '16 at 14:11