将声明的Const Name分配给Delphi中的var

时间:2016-01-17 20:23:03

标签: string delphi variables const names

有没有办法将一个声明的const名称分配给var ?,我有下面的代码,其中有一些Consts声明为字符串,这些字符串是JSON格式的图像但是根据所选值我要显示图像或另一个;

unit Images;

interface

Const
    Image1 = '[84,80,70,48,22,84,87,114,97,112,112,101,114,77,117,108,116,105,82,101, 48, 34]';
    Image2 = '[84,80,70,48,22,84,87,114,97,112,112,101,114,77,117,108,116,105,82,101, 90, 47]';
    Image3 = '[84,80,70,48,22,84,87,114,97,112,112,101,114,77,117,108,116,105,82,101, 22, 74]';

.
.
.
.
.
.
.

implementation

end.



unit MainForm;

interface

uses
   Images...

type
  TForm1 = class(TForm)
    Picture1: TImage;
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure ListBox1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure AsignJSONToImage(ImageString: String; DisplayImage: TImage);
  end;

var
  Form1: TForm1;

implementation

procedure TForm1.AsignJSONToImage(ImageString: String; DisplayImage: TImage);
var
  Stream: TStream;
  JsonArray: TJSONArray;
begin
    jsonArray := TJSONObject.ParseJSONValue(ImageString) as TJSONArray;
    try
        Stream := TMemoryStream.Create;
        Stream := TDBXJSONTools.JSONToStream(jsonArray);
        try
            Stream.Position := 0;
            DisplayImage.MultiResBitmap.LoadFromStream(Stream);
        finally
           Stream.Free;
        end;
    finally
        jsonArray.Free;
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
    ListBox1.Items.Add('Image1');
    ListBox1.Items.Add('Image2');
    ListBox1.Items.Add('Image3');
end;

procedure TForm1.ListBox1Change(Sender: TObject);
var
      Item: TListBoxItem;
begin
      Item := Listbox1.Selected;
      AsignJSONToImage(Item.Text, Picture1);  {* Here in the first parameter,I want to pass the name of the const selected in Listbox1 but not the text itself to display the image in Picture1*}
end;

end.

我希望你明白我想做什么,非常感谢你的帮助。

问候。

0 个答案:

没有答案