Pregunta

An Office Add-In I'm developing is going to have a few dozen 120-length double arrays in it (statically). I could just make a class with a bunch of static member arrays and use array initializers, but this seems a little ugly. It makes sense to me that I'd be able to store these in a Resource file, but it doesn't really have any options that fit. The closest option is "text file" but then I'd either have to parse each array each time I wanted to use it or build a lazyloader (which seems just as inelegant). Is there a better option?

(For the curious, the arrays are mortality tables.)

¿Fue útil?

Solución 2

Why not use a string resource. Read that string, seperate it on the comma's and parse each number. When you have read the string, all seperating and parsing can be done in one line of LINQ.

Example:

        string myResource = "1, 2, 3.4, 5.6";
        double[] values = myResource.Split(',').Select(Convert.ToDouble).ToArray();

Otherwise you could serialize your array with doubles once to a binary file and deserialize it in your code.

If parsing is your main concern (because of speed), store the result locally in a private cache. Each time the array is requested, just make a clone of it.

Otros consejos

I would personally make them static members of a static class and put the data in an ini file. Add a bool to the class to indicate whether or not the arrays have been initialized (or you could do this on an array by array basis) and in all files you access the class do a check to make sure it's initialized, if not, call the method which reads the file and loads data into the arrays. The method to load the data really shouldn't be that messy, and it's a fairly trivial operation.

This method also gives you namespace access to the data (global if you have the proper using statements/build dependencies).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top