Solved

assign variable name at runtime

Posted on 2002-03-09
7
926 Views
Last Modified: 2012-05-05
I'm creating a TDatabase component at runtime.

Example:
var
  DataBase1: TDatabase;
begin
  Database1:= TDataBase.Create(nil);
End;

How do I declare a variable name at runtime.  I don't
want my variable name to be DataBase1, instead I want
to assign a name to it at runtime.  How can I do this?

Thanks  
0
Comment
Question by:brettawv
7 Comments
 
LVL 2

Expert Comment

by:DelphiArchitect
ID: 6853327
I'm not following.  You can name DataBase1 anything you like such as MyDB:

var
  MyDB:TDatabase;
begin
  MyDB := TDatabase.Create(nil);
  try
    ...
  finally
    MyDB.Free;
  end;
end;

The variable name, itself, has no existance at runtime.  Are you by chance talking about the Database name that you need to assign to TTables, TQuerys, etc. to access this database?  If so you simply do:

  MyDB.DatabaseName := 'name';

Where name can be any valid database name (not certain if these can have spaces, etc).  Or you could do:

  MyDB.DatabaseName := 'temp_' + IntToStr(Random(65535));

Hope this helps.

Delphi Architect
James Higgins
0
 

Author Comment

by:brettawv
ID: 6853437
I'm writing a web app that accesses an AS400.  I have a component which allows me hooks into the AS400.  I use a database component and the databasename of the component is the alias used to give me access to the AS400.  When two people try to access the database at the same time the first person gets their response but the second person gets an error saying that the databasename is in use.
As long as the first persons request is completed before the second person submits their request, everything works fine.  What I'm trying to do is create another instance of the database that will run if the first one is not yet free.  I want to assign the sessionid as the variable name for the database before I create it.  This way I can keep track of which one is still active and which one is ready to be freed.  Is it possible to do this?
0
 
LVL 2

Accepted Solution

by:
craig_capel earned 100 total points
ID: 6853947
Var
  UsersConnected: Integer;

Example:
 var
   DataBase1: Array[1..10] of TDatabase;
   begin
    Inc(UsersConnected);
    Database1:= TDataBase[UsersConnected].Create(nil);
  End;


so you can create multiple instances of it as of and when you need it.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:craig_capel
ID: 6853953
Database1[UsersConnected]:= TDataBase.Create(nil);

oops sorry change that line for this. :)
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6854295
You cannot change the name of any variable at runtime.
0
 
LVL 17

Expert Comment

by:geobul
ID: 6854356
Hi,
There are two basic types of web apps that can be created with Delphi: CGI (exe) and ISAPI/NSAPI (dll). They both support internally what you are trying to do. Many copies of a CGI app are executed simultaneously as many times as there are requests received. On the other hand ISAPI is loaded once in memory and creates its copies on request or uses already created copies that are currently not in use.

So, make your app CGI or ISAPI and there won't be such a problem.

Regards, Geo
0
 

Author Comment

by:brettawv
ID: 6854704
Thanks, This will help me accomplish what I was trying to do.  I appreciate the help.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

697 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