IndexOfName (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

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 is replaced by the source�s value. TListBox objects that are not name-value pairs are not assigned a Name; IndexOfName will return -1. TStrings Values are referenced using the name string as an index.

Code

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);
}

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  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");
}

Uses