views:

149

answers:

4

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: 

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;
...
gath
Where does the '//Inflation record' part go in the Unit?
Chris55
At the type section of the unit
gath
Solved it! i took away the semi-colon after record. JIC anyone else has the same problem!
Chris55
Fixed the typo'd semicolon for future readers.
Ken White
+1  A: 

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

Jeroen Pluimers
Not voting down, because it's a valid answer. However, a CDS for a dozen records of three strings each seems like overkill to me.
Ken White
I use ClientDataSets with persistent fields even for single-row data sets, usually wrapped in a DataModule. It is so easy to use, and has the benefit of easy binding to a UI as well. Sample: configuration dialog with validation. Exports to XML config file with ease. With the new Generics in Delphi 2009 (finally stable in Delphi 2010) I expect a totally new class of solutions for this.
Jeroen Pluimers
+1  A: 
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';
Runner
+1  A: 

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];
Bruce McGee