inethog
asked on
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SetLength(Da,50); even sorry.
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?
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?
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,NumO fArray);
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;
Form1: TForm1;
dinArrayInt: Array Of Integer;
dinArrayLab: Array Of Tlabel;
procedure TForm1.Button1Click(Sender
var
i,NumOfArray: Integer;
begin
NumOfArray:=100;
SetLength(dinArrayInt,NumO
For i:=0 to 99 do
dinArrayInt[i]:=1;
end;
or
components
procedure TForm1.Button1Click(Sender
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
dinArray[i].left:=10+(I*5)
dinArray[i].top:=50+(i*5);
dinArray[i].Caption:='this
end;
end;
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...
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...
ASKER
tobjectpascal's answer will work for me...
Thanks for all of your inputs .
Regards.
Thanks for all of your inputs .
Regards.