Declaring variables at runtim

Is it possible to declare a variable at runtime ?

I want something like this :

procedure TForm1.SomeProc(Validated : Boolean);
begin
   if Validated then
   begin
      strList : TStringList;
      strList.Create(nil);
      // Do some stuff...
      strList.Free;
   end;
end;
sruelAsked:
Who is Participating?
 
robert_marquardtConnect With a Mentor Commented:
This is completely impossible for a compiled language.
Only a interpreted language can do that.

The above example does not need it.
strList is only a pointer. The object it points to IS created at runtime.
0
 
kretzschmarCommented:
not possible,
except you've an inbuild-compiler

but why you want this,
why not easy

procedure TForm1.SomeProc(Validated : Boolean);
var strList : TStringList;
begin
  if Validated then
  begin
     strList.Create(nil);
     // Do some stuff...
     strList.Free;
  end;
end;

as you have to code the logic also

meikl ;-)
0
 
kretzschmarCommented:
hi robert :-))
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
lucikaCommented:
procedure TForm1.SomeProc(Validated : Boolean);
begin
  if Validated then
  begin
     with TStringList.Create(nil) do
     begin
       // Do some stuff...
       Free;
     end;
  end;
end;
0
 
robert_marquardtCommented:
kretschmar, lucika: TStringList.Create not TStringList.Create(nil). TStringList is no component.
Shame on you ;-)
0
 
kretzschmarCommented:
>Shame on you ;-)
just copied

;-)
0
 
marcoszorrillaCommented:
Shame on What?
1. inthe: 504306 pts  
2. Madshi: 471558 pts  
3. kretzschmar: 422246 pts  

Best Regards.
Marcos.

0
 
kretzschmarCommented:
atleast, robert is right,
the creator of the tstringlist has no parameter,
but i've just copied the given code

shame on me :-))

meikl ;-)
0
 
sruelAuthor Commented:
So, I should give the points to wich one of you ?
0
 
kretzschmarCommented:
give it robert,
he was the first who pointed out,
that this is impossible

meikl ;-)
0
 
SteveWaiteCommented:
if you didn't know how many integers (say) you wanted then during run time a dynamic array of integers would suit nicely!

:-)
0
 
sruelAuthor Commented:
Ok, thanks to all of you folks !
0
All Courses

From novice to tech pro — start learning today.