BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

TQuery & TDataSource Help

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
Randel Bjorkquist
Guest





PostPosted: Fri Nov 21, 2003 2:55 pm    Post subject: TQuery & TDataSource Help Reply with quote



Hello all,

I need some help understanding why I get the following errors, "Can not
assign TQuery to TQuery" and "Can not assign TDataSource to TDataSource"
when I use the "TPersistent::Assign", as shown below as
tQuery->Assign(TmpQuery) or tDataSource->Assign(TmpSource). I use my base
class, TBaseData, when I create my other classes, like: Audit, AuditType,
Plant, Employee, etc...

In my base class, I have TQuery and TDataSource pointer pointing to their
respective components in a TDataModule. My inherited classes work fine if I
have only created one of those objects, like in my plant form. In my plant
form, the form's OnShow event creates a new TPlant object and the OnClose
event, deletes it. But if I try to have two or more objects in memory, I
run into problems with my TQuery & TDataSource pointers. So what I'm trying
to do is to change from pointing to the components in the TDataModule, but
still use them as a kind of template to creating new TQuery & TDataSource
components for each new instance of my objects.

If I'm mistaken and I can not use the "Assign" method, does anyone have any
other ideas how to do what I'm trying?

Thanks for any and all help in advance,

Randel Bjorkquist


//--------------------------------------------------------------------------
-
void __fastcall TBaseData::Set_DataModule(TDataModule* tModule)
{
:
Get_TQuery(FQuery, "qry_" + dmShortName);
Get_TDataSource(FDataSource, "ds_" + dmShortName);
:
}//end of "TBaseData::Set_DataModule()" method

//--------------------------------------------------------------------------
-
__fastcall TBaseData::TBaseData(void)
{
FQuery = new TQuery(NULL);
FDataSource = new TDataSource(NULL);
}//end of "TBaseData::TBaseData()" method

//--------------------------------------------------------------------------
-
__fastcall TBaseData::~TBaseData(void)
{
delete FQuery;
delete FDataSource;
}//end of "TBaseData::~TBaseData()" method

//--------------------------------------------------------------------------
-
TComponent* __fastcall TBaseData::Get_TComponent(AnsiString tName)
{
TComponent* TmpComponent = NULL;

for(int i = 0; i < FDataModule->ComponentCount; i++){
if(FDataModule->Components[i]->Name == tName){
TmpComponent = FDataModule->Components[i];
break;
}//end of "if-statement"
}//end of "for-loop

return(TmpComponent);
}//end of "TBaseData::Get_TComponent" method

//--------------------------------------------------------------------------
-
void __fastcall TBaseData::Get_TQuery(TQuery* tQuery, AnsiString tName)
{
TQuery* TmpQuery = new TQuery(NULL);
TmpQuery = dynamic_cast<TQuery*> (Get_TComponent(tName));

//THIS CAUSES THE ERROR "Can not assign TQuery to TQuery"
//***************************************************
tQuery->Assign(TmpQuery);
//***************************************************

delete TmpQuery;
TmpQuery = NULL;
}//end of "TBaseData::Get_TQuery()" method

//--------------------------------------------------------------------------
-
void __fastcall TBaseData::Get_TDataSource(TDataSource* tDataSource,
AnsiString tName)
{
TDataSource* TmpSource = new TDataSource(NULL);
TmpSource = dynamic_cast<TDataSource*> (Get_TComponent(tName));

//THIS CAUSES THE ERROR "Can not assign TDataSource to TDataSource"
//***********************************************************
tDataSource->Assign(TmpSource);
//***********************************************************

delete TmpSource;
TmpSource = NULL;
}//end of "TBaseData::Get_TDataSource()" method


Back to top
Todd Brylski
Guest





PostPosted: Sat Nov 22, 2003 9:50 am    Post subject: Re: TQuery & TDataSource Help Reply with quote



"Randel Bjorkquist" <rbjorkquist (AT) coilcraft (DOT) com> wrote

Quote:
I need some help understanding why I get the following errors, "Can not
assign TQuery to TQuery" and "Can not assign TDataSource to TDataSource"
when I use the "TPersistent::Assign", as shown below as
tQuery->Assign(TmpQuery) or tDataSource->Assign(TmpSource).

void __fastcall TBaseData::Get_TQuery(TQuery* tQuery, AnsiString tName)
{
TQuery* TmpQuery = new TQuery(NULL);
TmpQuery = dynamic_cast<TQuery*> (Get_TComponent(tName));

This isn't good. You are overwriting the TmpQuery pointer.
That will cause a memory leak and your new TQuery is inaccessible.
The dynamic_cast will either return a TQuery* or NULL.
It must be tested before it is used.


Quote:
//THIS CAUSES THE ERROR "Can not assign TQuery to TQuery"
//***************************************************
tQuery->Assign(TmpQuery);
//***************************************************

TQuery does not implement Assign, so it throws an EConvertError exception.
It would be difficult to know what it should assign.

Try something like this:

void __fastcall TBaseData::Get_TQuery(TQuery* tQuery, AnsiString tName)
{
TQuery* TmpQuery = dynamic_cast<TQuery*> (Get_TComponent(tName));
if( TmpQuery )
{
tQuery->DatabaseName = TmpQuery->DatabaseName;
tQuery->DataSource = TmpQuery->DatabaseSource;
...
}
else
{
throw Exception( tName + " is not a TQuery component!" );
}
}

The Get_TDataSource method has the same problems.

Todd



Back to top
Randel Bjorkquist
Guest





PostPosted: Tue Nov 25, 2003 1:26 pm    Post subject: Re: TQuery & TDataSource Help Reply with quote



Hey Todd,

Thanks for your reply. I see what you are talking about, and honestly don't
know why I did it that way to begin with. But, I am still trying to find a
way in which I can make a duplicate TQuery or TDataSource component from an
existing one.

What I was hoping to be able to do was take a TDataModule, place all the
general components I wanted on it and be able to "copy" it, kind of use it
like a template (not a C++ Template, but more like a Microsoft Word/Excel
template). So I was hoping to be able to create a function in which I could
pass it the name of a TQuery or TDataSource and have it return a pointer to
the new "copy" / duplicate.

Thanks again and if you have any other ideas, please feel free to let me
know.

Randel

"Todd Brylski" <tbrylski (AT) yyaahhoooo (DOT) com> wrote

Quote:
"Randel Bjorkquist" <rbjorkquist (AT) coilcraft (DOT) com> wrote

I need some help understanding why I get the following errors, "Can not
assign TQuery to TQuery" and "Can not assign TDataSource to TDataSource"
when I use the "TPersistent::Assign", as shown below as
tQuery->Assign(TmpQuery) or tDataSource->Assign(TmpSource).

void __fastcall TBaseData::Get_TQuery(TQuery* tQuery, AnsiString tName)
{
TQuery* TmpQuery = new TQuery(NULL);
TmpQuery = dynamic_cast<TQuery*> (Get_TComponent(tName));

This isn't good. You are overwriting the TmpQuery pointer.
That will cause a memory leak and your new TQuery is inaccessible.
The dynamic_cast will either return a TQuery* or NULL.
It must be tested before it is used.


//THIS CAUSES THE ERROR "Can not assign TQuery to TQuery"
//***************************************************
tQuery->Assign(TmpQuery);
//***************************************************

TQuery does not implement Assign, so it throws an EConvertError exception.
It would be difficult to know what it should assign.

Try something like this:

void __fastcall TBaseData::Get_TQuery(TQuery* tQuery, AnsiString tName)
{
TQuery* TmpQuery = dynamic_cast<TQuery*> (Get_TComponent(tName));
if( TmpQuery )
{
tQuery->DatabaseName = TmpQuery->DatabaseName;
tQuery->DataSource = TmpQuery->DatabaseSource;
...
}
else
{
throw Exception( tName + " is not a TQuery component!" );
}
}

The Get_TDataSource method has the same problems.

Todd





Back to top
Todd Brylski
Guest





PostPosted: Tue Nov 25, 2003 4:31 pm    Post subject: Re: TQuery & TDataSource Help Reply with quote

"Randel Bjorkquist" <rbjorkquist (AT) coilcraft (DOT) com> wrote


Quote:
Thanks for your reply. I see what you are talking about, and honestly don't
know why I did it that way to begin with. But, I am still trying to find a
way in which I can make a duplicate TQuery or TDataSource component from an
existing one.

What I was hoping to be able to do was take a TDataModule, place all the
general components I wanted on it and be able to "copy" it, kind of use it
like a template (not a C++ Template, but more like a Microsoft Word/Excel
template). So I was hoping to be able to create a function in which I could
pass it the name of a TQuery or TDataSource and have it return a pointer to
the new "copy" / duplicate.

Thanks again and if you have any other ideas, please feel free to let me
know.

Why can't you just create a new TQuery and copy over the properties that you want?
That's is what assign would do, but the question is what if the TQuery
is active? I don't think that you would want to execute it as is?
I would think that you would want to have a new SQL statement with the new Query.

You could always create a new component derived from TQuery and implement the
Assign method. You would have to drop the new TQuery on the Module though.

Todd



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.