RAD Studio VCL Reference
|
Indicates the name part of strings that are name-value pairs.
property Names [Index: Integer]: string;
__property AnsiString Names[int Index];
When the list of strings for the TStrings object includes strings that are name-value pairs, read Names to access the name part of a string. Names is the name part of the string at Index, where 0 is the first string, 1 is the second string, and so on. If the string is not a name-value pair, Names contains an empty string.
C++ Examples:
/* The following example updates the strings in a list box given the strings contained in another list box. If a string in the source list box has the form Name=Value and the destination list box contains a string with the same Name part, the Value part in the destination list box will be replaced by the source’s value. TListBox objects that are not name-value pairs are not assigned a Name and IndexOfName will return -1. TStrings Values are referenced using the name AnsiString as an index. */ void MergeStrings(TStrings *Dest, TStrings *Source) { for (int i = 0; i < Source->Count; i++) { if (Source->Strings[i].Pos("=") > 1) { int DI = Dest->IndexOfName(Source->Names[i]); if (DI > -1) Dest->Values[Source->Names[i]] = Source->Values[Source->Names[i]]; } } } void __fastcall TForm1::Button1Click(TObject *Sender) { MergeStrings(ListBox1->Items,ListBox2->Items); } void __fastcall TForm1::FormCreate(TObject *Sender) { ListBox1->Items->Add("Plants = 10"); ListBox1->Items->Add("Animals = 20"); ListBox1->Items->Add("Minerals = 15"); ListBox2->Items->Add("Animals = 4"); ListBox2->Items->Add("Plants = 3"); ListBox2->Items->Add("Minerals = 78"); }
Delphi Examples:
{ The following example updates the strings in a list box given the strings contained in another list box. If a string in the source list box has the form Name=Value and the destination list box contains a string with the same Name part, the Value part in the destination list box will be replaced by the source’s value. TListBox objects that are not name-value pairs are not assigned a Name and IndexOfName will return -1. } procedure MergeStrings(Dest, Source: TStrings); var I, DI: Integer; begin for I := 0 to Source.Count - 1 do begin if Pos ('=', Source[I]) > 1 then begin DI := Dest.IndexOfName(Source.Names[I]); if DI > -1 then Dest[DI] := Source[I]; end; end; end; procedure TForm1.Button1Click(Sender: TObject); begin MergeStrings(ListBox1.Items, ListBox2.Items); end; procedure TForm1.FormCreate(Sender: TObject); begin ListBox1.Items.Add('Plants = 10'); ListBox1.Items.Add('Animals = 20'); ListBox1.Items.Add('Minerals = 15'); ListBox2.Items.Add('Animals = 4'); ListBox2.Items.Add('Plants = 3'); ListBox2.Items.Add('Minerals = 78'); end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|