Saving a set of values ​​in Delphi

I'm trying to store a set of values ​​in delphi, but I want to be able to address them using their name instead of the assigned number.

For example, the "OldValues" array would allow me to do

OldValue[1] := InflationEdit.Text;

      

Ideally, however, I would like to have the value stored in "Data.Inflation.OldValue" for example. For every identifier like inflation, there are OldValue, NewValue and StoredValue. There are about 12 of these identifiers.

Is there a way to store these values? this way I could do something like:

Data.Inflation.NewValue := Data.Inflation.OldValue;
Data.Inflation.NewValue := InflationEdit.Text;

      

+1


a source to share


4 answers


The class would really really like it in this problem. something like;



// Inflation record
TInflation = record
  NewValue,
  OldValue:string;
end;

/ data class
Tdata = class(object)
private
  Inflation:TInflation;
  // other properties
  ...
public
 constructor ...

end;

data := TData.create(nil)

data.Inflation.NewValue :=  data.inflation.OldValue;
...

      

+2


a source


This might work for you:

  • Datasets have fields.
  • The fields are OldValue and Value.
  • Fields can be persistent (that's why they are declared and you have code completion).
  • TClientDataSet (which is a DataSet) is basically an internally stored table having zero or more records.


- Jeroen

+1


a source


type
  TIndentifier = class
  private
    FOldValue: string;
    FNewValue: string;
    FStoredValue: string;
  public
    constructor Create(const OldValue, NewValue, StoredValue: string); 
    property OldValue: string read FOldValue write FOldValue;
    property NewValue: string read FNewValue write FNewValue;
    property StoredValue: string read FStoredValue write FStoredValue;
  end;

      

This is your base class. You use it for each value. Then you have two options. You can simply do it like this:

var
  Values: TStringList;
begin
  Values.AddObject('SomeValue', TIndentifier.Create('OldValue', 'NewValue', 'StoredValue'));

      

This reminds me of how similar it is to the Swiss knife of TStringList :). Watch out to free objects in older versions of delphi, or set TStringList as the owner of objects in recent versions.

Or you can have a list of objects if you need more than just a name:

   type   
      TValue = class(TIndentifier) 
      private
        FName: string;   
      public
        property Name: string read FName write FName;    
      end;

    var
      Value: TValue;    
      Values: TObjectList;   
    begin   
      Value := TValue.Create('OldValue', 'NewValue', 'StoredValue');   
      Value.Name := 'SomeValue';  
      Values.Add(Value);

      

But what I really like about such cases is the power of XML. This saves so much code, but on the other hand hides the declarations. With SimpleStorage, you can do something like this:

    var
      Value: IElement;
      Storage: ISimpleStorage:
    begin
      Storage := CreateStorage;
      Value := Storage.Ensure(['Values', 'SomeValue']); 
      Value.Ensure('OldValue').AsString := 'OldValue';
      Value.Ensure('NewValue').AsString := 'NewValue';
      Value.Ensure('StoredValue').AsString := 'StoredValue';

      

+1


a source


Which version of Delphi? If it's Delphi 2009 or newer, you can use TDictionary .

Store your name as a key and an object with OldValue and NewValue as value.

MyDictionary: TDictionary<string, TMyClass>;

      

Find the given TMyClass instance by name:

MyDictionary.Items[SomeName];

      

+1


a source







All Articles