Mega Code Archive

 
Categories / Delphi / Strings
 

Extended TStringList

Title: Extended TStringList Question: How to use TStringList AddObject Effectively Answer: In our application there are situation where we are using many time TStringlist.AddObject for example in 1 unit I have to use it then in that unit i have to creatr a class type TFloatVal = Class code: String; value: Integer; sum: Real; end then to use above class everytime i have to create an Object say object: TFloatVal; object:= TFloatVal.Create; object.code := somestring; object.value := someInteger; object.sum := someFloat; stringList.AddObject(somestring, object); This don't end here In our Application many places we have to face this situation, so evreytime we have to do the same process So why dont we use TExtendedStringList type TExtendedStringList = class (TStringList) private ValList : TStringList; NameArr : array of string; public constructor Create(ANamearray: array of string); destructor Destroy; override; procedure AddObjects(const S: string; AValues: array of string); function GetObjectValuesbyName(const S: string; AName: String):string; procedure Clear; override; end; { TExtendedStringList } // Adding Objects values are in array procedure TExtendedStringList.AddObjects(const S: string; AValues: array of string); var i: Integer; begin ValList := TStringList.Create; for i:= low(NameArr) to High(NameArr) do ValList.Add(NameArr[i] + '=' + AValues[i]); AddObject(S, TObject(ValList)); end; // while clearing freeing all the object that stringlist holds procedure TExtendedStringList.Clear; var i: Integer; begin for i:= Count-1 downto 0 do begin TStringList(Objects[i]).Free; end; inherited; end; // Creating with the parameter array of string constructor TExtendedStringList.Create(ANamearray: array of string); var i : Integer; begin setLength(NameArr, Length(ANamearray)); for i:= low(ANamearray) to High(ANamearray) do NameArr[i] := ANamearray[i]; inherited Create; end; // Here we are freeing all the object destructor TExtendedStringList.Destroy; var i: Integer; begin for i:= Count-1 downto 0 do begin TStringList(Objects[i]).Free; end; inherited; end; //Here we are returing the value of the name function TExtendedStringList.GetObjectValuesbyName(const S: string; AName: String): string; var indx: Integer; begin indx := IndexOf(S); if (indx = -1) or (ValList.IndexOfName(AName) = -1) then begin Result := SysUtils.EmptyStr; Exit; end; Result := TStringList(Objects[indx]).Values[AName] ; end; Use of above class procedure TForm1.Button1Click(Sender: TObject); var sum,sum1,sum2: Real; shallist: TExtendedStringList; begin shallist := TExtendedStringList.Create(['Sum','sum1','sum2']) ; sum := 10.99; sum1 := 11; sum2:= 25; shallist.AddObjects('AA', [Floattostr(sum),Floattostr(sum1),Floattostr(sum2)]); sum := 12.99; sum1 := 21; sum2:= 45; shallist.AddObjects('BB', [Floattostr(sum),Floattostr(sum1),Floattostr(sum2)]); showmessage(shallist.GetObjectValuesbyName('AA', 'sum2')); shallist.Free; end; Any Comment and suggestion are welcome ya we can make it more effective.