Solved

assign variable name at runtime

Posted on 2002-03-09
7
920 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now