Storing a set of values in Delphi

别来无恙 提交于 2019-12-08 08:10:18

问题


I am trying to store a set of values in delphi, but i want to be able to address them using their name, instead of an assigned number.

For example, an array of 'OldValues' would let me do

OldValue[1] := InflationEdit.Text;

Ideally however, i would like to have a value stored in 'Data.Inflation.OldValue' for example. For each identifier, such as Inflation, there is an OldValue, NewValue and StoredValue. There are about 12 of these identifiers.

Is there some way i could store the values like this? that way i could then do something like:

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

回答1:


A class would really came very handy on this kind of problem. something in the likes of;

// 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:


This might work for you:

  • DataSets have Fields.
  • Fields have OldValue and Value.
  • Fields can be persistent (so they are declared and you have code completion on them).
  • TClientDataSet (which is a DataSet) is basically just an in-memory table having zero or more records.

--jeroen




回答3:


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 just do it like this:

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

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

Or you can have an list of objects if you need more than just 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 in such cases is the power of XML. It saves so much code, but hides the declarations on the other hand. With my SimpleStorage you could 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';



回答4:


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

Store your name as a the Key and an object with OldValue and NewValue members as the Value.

MyDictionary: TDictionary<string, TMyClass>;

Search for a given instance of TMyClass by name:

MyDictionary.Items[SomeName];


来源:https://stackoverflow.com/questions/2628339/storing-a-set-of-values-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!