Dynamically Created Variables in Delphi at Runtime

Is there a way to dynamically create a variable ( or series thereof ) and access them
at runtime?

ie:  The program starts, we click a button on the main form, the variables then get
dynamically created.

After which, we click say another button, and values are assigned to said variables...

And clicking another button would display for us the assigned values to the dynamically
created variables...

Variable type is not important, as I'm only looking for the method in dynamically creating them.

Also, I'm not interested in creating components at runtime, as I already know how to do this.

Thanks for your help,

D.
LVL 1
inethogAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tobjectpascalCommented:
var
  Form1: TForm1;
  DA: Array Of Integer;

procedure TForm1.Button1Click(Sender: TObject);
Var
 N: Integer;
begin
  SetLength(DA,11);
  For N:=1 to 10 do
     DA[N]:=1;
end;

if you want 50 integers, you do SetLength(DA,49); keep in mind, that 0 is the starting point not 1, just that i never liked using 0, reminds me of C too much
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tobjectpascalCommented:
SetLength(Da,50); even sorry.
0
rbohacCommented:
I'm not sure I quite understand your question, but you need to have a way to track the variables. Typically this would be done either though an array (like tobjectpascal said) or through a list of some sort like TList or TThreadList

You can always allocate and use memory (through getmem and freemem) using pointers.

Is this what you are trying to do? Can you elaborate a little?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

nikola_mkCommented:
var
  Form1: TForm1;
  dinArrayInt: Array Of Integer;
  dinArrayLab: Array Of Tlabel;


procedure TForm1.Button1Click(Sender: TObject);
var
 i,NumOfArray: Integer;
begin
NumOfArray:=100;
     SetLength(dinArrayInt,NumOfArray);
     For i:=0 to 99 do
         dinArrayInt[i]:=1;
end;

or

components


procedure TForm1.Button1Click(Sender: TObject);
var
 i,NumOfArray: Integer;
begin
NumOfArray:=100;
     SetLength( dinArrayLab,NumOfArray);
     For i:=0 to 99 do
     beign
         dinArray[i].Parent:=Form1;
         dinArray[i]:=Tlabel.Create(Self);
         dinArray[i].left:=10+(I*5);
         dinArray[i].top:=50+(i*5);
         dinArray[i].Caption:='this label: '+Inttostr(i);

    end;
end;


0
Wim ten BrinkSelf-employed developerCommented:
Use the variant type. It can hold any data type.
Also an option, use pointers. Something simple as:

var
  AValue: ^Integer;
begin
  New(AValue); // Creates the variable
  AValue^ :=10; // Assigns a value
  AValue^ := AValue^ * 25; // Multiplies value with 25
  Dispose(AValue); // Destroys the variable again

By using pointers you can easily allocate and free "variables", although the name variable isn't really appropiate. Basically, you just allocate and free memory this way.

Now, you probably want to do more than just store a single variable. You want a list of user-defined variables. You could of course use this:

type
  TVariableTypes = ( vtString, vtInteger, vtBoolean );
  TVariableRecord = record
    VarName: string;
    case VarType: TVariableTypes of
      vtString: ( VarString: ShortString );
      vtInteger: ( VarInteger: Integer );
      vtBoolean: ( VarBoolean: Boolean );
  end;
  TVariableList = array of TVariableRecord;

var
  VariableList: TVariableList;

This would give you a dynamic list of variables that you can add and remove in runtime. All you have to do when you need to look up a variable is search in this list for the right name. However, this solution has a problem since variant records don't allow normal strings in them or other objects. Then again, you could use

  TVariableRecord = record
    VarName: string;
    VarType: TVariableTypes;
    VarPointer: Pointer;
  end;

And use the pointer technology again that I mentioned before. But this requires more code and a few checks to make sure you're not doing wrong typecasts. You'd have to create a component wrapper around this to work with this nicely...
0
inethogAuthor Commented:
tobjectpascal's answer will work for me...

Thanks for all of your inputs .

Regards.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.