Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

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;
0
sruel
Asked:
sruel
  • 5
  • 2
  • 2
  • +3
1 Solution
 
robert_marquardtCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now