Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object vs Exists vs Creating 1

Status
Not open for further replies.

ddverne

Programmer
Dec 29, 2011
9
BR
Hi everybody,

I'm having a little trouble to check if an object was already created.
I tried some examples that I've found on google but without any lucky.

Example:

procedure TForm1.TestClick(Sender: TObject);
var
conn : TADOConnection;
begin
try
if not Assigned(conn) then
conn := TADOConnection.Create(self);

conn.ConnectionString := '';
except
on E : Exception do
begin
ShowMessage('Error: '+E.Message);
end;
end;
end;

On the code above: the conn ":= TADOConnection.Create(self);" never happens, and I get access violation on "conn.ConnectionString := '';"

I have been trying comparisons with nil without success, because the conn is not nil it has an andress.

Thanks.
 
Why have Conn as a Local varaible to a procedure? You are much better off having it as a member of a DataModule; then it will be visible to any module in the application that needs it and the application can manage it much better. You just include the Datamodule in the uses module. (I should have noticed this from your previous post; My apologies). This also allows you to insure that it is created before you access it, all you have to do is check the connected property before using it. If it is not connected, then you set connected to true, as long as you have all the parameters and command strings entered in during design time. The following assumes you have a datamodule in your project called DataModule1, and it contains a TADOConnection called Conn that has already been setup with its connection string.

Code:
procedure TForm1.TestClick(Sender: TObject);
begin
    try
        if not DataModule1.Conn.Connected then
            Conn.Connected := true;
   except
        on E : Exception do
        begin
            ShowMessage('Error: '+E.Message);
        end;
    end;
end;
 
Hi Prattaratt,

In fact I'm already using a global unit for connection and other things. The code above is just an example, in my main code I'll not declare it locally.

My main point on this example is how can I do to know if the the object is already created. (In this case I'm using TADOConnection to understand).

How can I know if an Object (for an exemple TADOConnection) is already created?
 
If it is an object in another Unit (like TADOConnection in a DataModule1) you set the creation order of the Forms in the main app unit. you can pull this up by:

1. opening up the project options and clicking on forms. you can drag the form to the appropiate order. This will be the creation order for the Forms.

2. You can also change the creation order by open up the exe source. You do this by right clicking on the project.exe in the project explorer window and select shows source. There you will see lines like the following:

Code:
program TestVCL;

uses
  Vcl.Forms,
  Main in 'Main.pas' {Form1},
  DataModule in 'DataModule.pas' {DataModule1: TDataModule};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TDataModule1, DataModule1);
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

Any global objects created in between the lines of Application.Initialize and Application.Run will be guaranteed to be created at runtime. You just need to insure they are destroyed after the Application.Run line.
If you are looking for a test of whether an object is created or not during runtime, I am not sure how to do that in Delphi (yet). In C++ Builder, I just make sure all my objects are initialized to NULL (NIL in Delphi) and ensure I set them back to NULL when I destroy them. I much prefer to take the guesswork out of it and know for certain an object's state with regard to creation at any given execution point. I will research some more to see what I can come up with.
 
Hi Prattaratt,

You gave me a great help when you told about your C++ initialization.

I did the same thing on my TADOConnection reference:

// Global
var
conn := TADOConnection = nil;

And now I know the condition.

Well I'm a little rusty with Delphi, since I don't use it since last year (2010). But I'm doing a new project and I want to do things differently from what I usually did back then, and I want to go deeper with this language/rad.

Do you have a blog? You are very technical.

Thank you again,

DDVerne.
 
No, I do not have a blog. I am just a longtime Borland/ Codegear/ Embarcadero semi-professional programmer. Learned Turbo Pascal 5.0 in college (I hope that does not date me too much) and fell in love.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top