开发者

linking 4 pieces of information and saving them

Savin开发者_运维技巧g, editing and loading information. The information that I want to load is something I will add myself. Each line of information will contain 4 pieces, (string, integer, string, integer). Via 4 seperate edit boxes and a button I will add this information to a 'database' (not sure if I need a database or if it can be done via something like a Tstringlist). Everytime the button is clicked it will added the content that is typed at that moment in the 'database'.

The only demand of the saved data is when I type the first string from the list it could place the rest of the information that belongs to it in a memobox or edit boxes as well. So I suppose I have to be able to search. Just want to keep it as simple as possible. There will only be about 10 to 15 lines of information. and if possible it would be good if I can load them again a later time.


Here's some very basic code that should get you on your way. There's no error checking, and you'll no doubt want to develop it and modify it further. The point is that there should be some ideas to help you write code that works for you.

Now that I have comma-separated the fields, but made no attempt to handle the appearance of commas in any of the values. If this is a problem then choose a different delimiter, or escape the commas. I had toyed with writing each field on its own line (effectively using a newline as the separator), but this makes the reading code more tricky to write.

Again, the main point is that this is not final production code, but is intended to give you a starting point.

function Split(const s: string; Separator: char): TStringDynArray;
var
  i, ItemIndex: Integer;
  len: Integer;
  SeparatorCount: Integer;
  Start: Integer;
begin
  len := Length(s);
  if len=0 then begin
    Result := nil;
    exit;
  end;

  SeparatorCount := 0;
  for i := 1 to len do begin
    if s[i]=Separator then begin
      inc(SeparatorCount);
    end;
  end;

  SetLength(Result, SeparatorCount+1);
  ItemIndex := 0;
  Start := 1;
  for i := 1 to len do begin
    if s[i]=Separator then begin
      Result[ItemIndex] := Copy(s, Start, i-Start);
      inc(ItemIndex);
      Start := i+1;
    end;
  end;
  Result[ItemIndex] := Copy(s, Start, len-Start+1);
end;

type
  TValue = record
    i1, i2: Integer;
    s: string;
  end;

  TMyDict = class(TDictionary<string,TValue>)
  public
    procedure SaveToFile(const FileName: string);
    procedure LoadFromFile(const FileName: string);
  end;

{ TMyDict }

procedure TMyDict.SaveToFile(const FileName: string);
var
  Strings: TStringList;
  Item: TPair<string,TValue>;
begin
  Strings := TStringList.Create;
  Try
    for Item in Self do begin
      Strings.Add(Format(
        '%s,%s,%d,%d',
        [Item.Key, Item.Value.s, Item.Value.i1, Item.Value.i2]
      ));
    end;
    Strings.SaveToFile(FileName);
  Finally
    FreeAndNil(Strings);
  End;
end;

procedure TMyDict.LoadFromFile(const FileName: string);
var
  Strings: TStringList;
  Item: TPair<string,TValue>;
  Line: string;
  Fields: TStringDynArray;
begin
  Strings := TStringList.Create;
  Try
    Strings.LoadFromFile(FileName);
    for Line in Strings do begin
      Fields := Split(Line, ',');
      Assert(Length(Fields)=4);
      Item.Key := Fields[0];
      Item.Value.s := Fields[1];
      Item.Value.i1 := StrToInt(Fields[2]);
      Item.Value.i2 := StrToInt(Fields[3]);
      Add(Item.Key, Item.Value);
    end;
  Finally
    FreeAndNil(Strings);
  End;
end;

Note that you don't attempt to search the file on disk. You simply load it into memory, into the dictionary and look things up from there.

A dictionary is great when you always use the same key. If you have multiple keys then a dictionary is less convenient, but who cares about the performance impact if you've only got 15 records?!

Disclaimer: I've not run the code, I've not tested it, etc. etc.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜