?
Solved

Dynamically Created Variables in Delphi at Runtime

Posted on 2004-11-29
6
Medium Priority
?
1,482 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:inethog
6 Comments
 
LVL 4

Accepted Solution

by:
tobjectpascal earned 2000 total points
ID: 12701594
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
 
LVL 4

Expert Comment

by:tobjectpascal
ID: 12701599
SetLength(Da,50); even sorry.
0
 
LVL 6

Expert Comment

by:rbohac
ID: 12702495
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Expert Comment

by:nikola_mk
ID: 12702559
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
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12704864
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
 
LVL 1

Author Comment

by:inethog
ID: 12708262
tobjectpascal's answer will work for me...

Thanks for all of your inputs .

Regards.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question