开发者

Best way of storing an "array of records" at design-time

开发者 https://www.devze.com 2022-12-23 18:58 出处:网络
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.

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
  TVulnerabilityData = record
    Vulnerability: TVulnera开发者_StackOverflowbility;
    Name: string;
    Description: string;
    ErrorMessage: string;
  end;

What's the best way of storing this data at design-time for later retrieval at run-time? I'll have about 20 records for which I know all the contents of each "record" but I'm stuck on what's the best way of storing the data.

The only semi-elegant idea I've come up with is "construct" each record on the unit's initialization like this:

var
  VulnerabilityData: array[Low(TVulnerability)..High(TVulnerability)] of TVulnerabilityData;

....

initialization
  VulnerabilityData[0].Vulnerability := vVulnerability1;
  VulnerabilityData[0].Name := 'Name of Vulnerability1';
  VulnerabilityData[0].Description := 'Description of Vulnerability1';
  VulnerabilityData[0].ErrorMessage := 'Error Message of Vulnerability1';

  VulnerabilityData[1]......
  .....
  VulnerabilityData[20]......

Is there a better and/or more elegant solution than this?

Thanks for reading and for any insights you might provide.


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.


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号