Solved

assign variable name at runtime

Posted on 2002-03-09
7
923 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

775 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