Difficulty creating a Property for a CLASS

Asked

Viewed 202 times

0

I am using DELPHI 7 trying to create a class with the following structure

Type
  Tclasse_Envio_JSON =  class(TThread)
  private  
    fCampos: Array of Variant;
    fIdade : Integer;
    fNome  : String[40];
    function Get_Idade: Integer;
    procedure Set_Idade(const Value: Integer);    
    function Get_Campos: array of Variant;
    procedure Set_Campos(const Value: array of Variant);         
  public
    Property Idade : Integer read Get_Idade write Set_Idade;    
    Property Campos : Array of Variant read Get_Campos write Set_Campos;
  protected
    procedure Execute; Override;      
end;

I’m having trouble implementing the:

property Campos
function Get_Campos 
procedure Set_Campos

The idea of its use would be

variavelA       := TClasse_Envio_JSon.Create;
VariavelA.Campo := ['CampoA','CampoB','CampoC'];
VariavelA.Valor :=['Valor_CampoA','Valor_CampoB','Valor_CampoC'];

How should I use the ARRAY Of VARIANT in that situation???
Or If any of you have another idea that I can get the same result. I am all EYES :)
Could Do something with delimited STRINGS and do the search on them, but the bid with Array of Variant is much cleaner.
We use a lot of Array of Variant in passing parameters in functions. Inclusive This class I’m looking to implement is to sub-create a function.

Function Envia_Json(pCampos : Array of Variant; pValores : Array of Variant) : Boolean;
Var A : Integer;
begin
  For A:= 0 to High(pCampos) do
      begin
         ETC..ETC..
      end;
  ETC.. ETC..
end;

Using the Send Function..:

  If Envia_Json(['CampoA','CampoB'],['ValorAA','VAlorA','ValorB','ValorC','Valord']) then
     begin
     end;

I hope I have demonstrated my doubt and need clearly..
If any of you find my lines confusing, please write to me and I will try to explain better.

1 answer

1

I already think of something simpler to solve, both in sending and receiving...

To send imagine a List (can be a Tstringlist), key and value...

To receive would use:

var
  vJson : TJSONObject;
begin
  vJson := TJSONObject.Create;  

  for i := 0 to Pred(xList.Count) do
  begin
    vJson.AddPair(xListaCHAVE, xListaVALOR);
  end;

  Result := vJson.ToString;
  vJson.Free;
end;

Note: For the TJSONObject declare Data.DBXJSON for older versions, for new versions use System.Json

  • Good morning @Junior Moreira. First I want to thank you for your attention :). A few remarks. Correct me if I’m wrong okay?? Pred(varTstringList) Does this work or did you write it wrong??? DATA.DBXJSON I tried it on Delphi 7 and didn’t find it.. I think Delphi 7 is older than the correct JSON?? I can even pass as Stringlist.. but this entails having to create Stringlist variables before using the class... if there is no other solution .. will be that same :) hugs :)

  • Pred(varX.Count) is equal to varX.Count -1, about DATA.DBXJSON, I believe it may not really exist for this version, but look on github about superobject a giant class that has JSON.

  • Moreira BRIGADUUU :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.