Best way of storing an “array of records” at design-time

后端 未结 3 1449
独厮守ぢ
独厮守ぢ 2021-01-15 00:25

I have a set of data that I need to store at design-time to construct the contents of a group of components at run-time.

Something like this:

type
           


        
相关标签:
3条回答
  • 2021-01-15 01:11

    The typical way would be a file, either properties style (a=b\n on each line) cdf, xml, yaml (preferred if you have a parser for it) or a database.

    If you must specify it in code as in your example, you should start by putting it in something you can parse into a simple format then iterate over it. For instance, in Java I'd instantiate an array:

    String[] vals=new String[]{
        "Name of Vulnerability1", "Description of Vulnerability1", "Error Message of Vulnerability1",
        "Name of Vulnerability2", ...
    }
    

    This puts all your data into one place and the loop that reads it can easily be changed to read it from a file.

    I use this pattern all the time to create menus and for other string-intensive initialization.

    Don't forget that you can throw some logic in there too! For instance, with menus I will sometimes create them using data like this:

    "^File", "Open", "Close", "^Edit", "Copy", "Paste"
    

    As I'm reading this in I scan for the ^ which tells the code to make this entry a top level item. I also use "+Item" to create a sub-group and "-Item" to go back up to the previous group.

    Since you are completely specifying the format you can add power later. For instance, if you coded menus using the above system, you might decide at first that you could use the first letter of each item as an accelerator key. Later you find out that File/Close conflicts with another "C" item, you can just change the protocol to allow "Close*e" to specify that E should be the accelerator. You could even include ctrl-x with a different character. (If you do shorthand data entry tricks like this, document it with comments!)

    Don't be afraid to write little tools like this, in the long run they will help you immensely, and I can turn out a parser like this and copy/paste the values into my code faster than you can mold a text file to fit your example.

    0 讨论(0)
  • 2021-01-15 01:13

    You can also declare your array as consts and initialize it...

    const
      VulnerabilityData: array[Low(TVulnerability)..High(TVulnerability)] of TVulnerabilityData =
    ( 
        (Vulnerability : vVulnerability1; Name : Name1; Description : Description1;  ErrorMessage : ErrorMessage1),
        (Vulnerability : vVulnerability2; Name : Name2; Description : Description2;  ErrorMessage : ErrorMessage2),
    [...]
        (Vulnerability : vVulnerabilityX; Name : NameX; Description : DescriptionX;  ErrorMessage : ErrorMessageX)
        )
    );
    

    I don't have an IDE on this computer to double check the syntax... might be a comma or two missing. But this is how you should do it I think.

    0 讨论(0)
  • 2021-01-15 01:16

    not an answer but may be a clue: design-time controls can have images and other binary data associated with it, why not write your data to a resource file and read from there? iterating of course, to make it simpler, extensible and more elegant

    0 讨论(0)
提交回复
热议问题