 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Maurice Anderson Guest
|
Posted: Sat Apr 28, 2007 11:38 pm Post subject: TCollection gives an error: why? |
|
|
I get an access violation when I click this button, why?
void __fastcall TForm1::Button5Click(TObject *Sender)
{
TCollection* FItems = new TCollection(NULL);
FItems->Add();
} |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Sun Apr 29, 2007 12:18 am Post subject: Re: TCollection gives an error: why? |
|
|
"Maurice Anderson" <mauriceanderson (AT) hotmail (DOT) com> wrote in message
news:4633948e$1 (AT) newsgroups (DOT) borland.com...
| Quote: | I get an access violation when I click this button, why?
|
Because you did not specify the class type that the collection holds
and which Add() creates instances of. The ItemClass parameter of
TCollection's constructor is a required parameter. It cannot be set
to NULL. For example:
void __fastcall TForm1::Button5Click(TObject *Sender)
{
TCollection* FItems = new
TCollection(__classid(TSomeClassDerivedFromTCollectionItem));
FItems->Add();
delete FItems;
}
Gambit |
|
| Back to top |
|
 |
Maurice Anderson Guest
|
Posted: Sun Apr 29, 2007 1:52 am Post subject: Re: TCollection gives an error: why? |
|
|
The ItemClass parameter of TCollection's constructor is a required
parameter. It cannot be set to NULL. For example:
| Quote: |
void __fastcall TForm1::Button5Click(TObject *Sender)
{
TCollection* FItems = new
TCollection(__classid(TSomeClassDerivedFromTCollectionItem));
FItems->Add();
delete FItems;
}
|
Ok, that works. But when I do this:
class PACKAGE TDataCollectionItem : public TCollectionItem
{
private:
Variant FData;
void __fastcall SetData(Variant value);
Variant __fastcall GetData();
protected:
public:
__fastcall TDataCollectionItem(TComponent* Owner);
__published:
__property Variant Data = { read=GetData, write=SetData };
};
---- form1 cpp -----
TDataCollectionItem *AnItem ;
(TCollectionItem *)AnItem = FItems->Add();
ShowMessage("AnItem: " + String(AnItem->Index));
I get an error. ?? |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue May 01, 2007 12:18 am Post subject: Re: TCollection gives an error: why? |
|
|
"Maurice Anderson" <mauriceanderson (AT) hotmail (DOT) com> wrote in message
news:4633b3ef$1 (AT) newsgroups (DOT) borland.com...
| Quote: | __fastcall TDataCollectionItem(TComponent* Owner);
|
That is the wrong constructor type. TCollectionItem's constructor
takes a pointer to a TCollection, not a TComponent. That makes a big
difference.
| Quote: | TDataCollectionItem *AnItem ;
(TCollectionItem *)AnItem = FItems->Add();
|
You should not be casting your target variable like that. Cast the
return value of Add() instead, ie:
TDataCollectionItem *AnItem;
AnItem = (TDataCollectionItem*) FItems->Add();
Since you are creating your own TCollectionItem class, you should be
creating your own TCollection class as well, ie:
class PACKAGE TDataCollection : public TOwnedCollection
{
typedef TOwnedCollection inherited;
private:
TDataCollectionItem* __fastcall GetDataItem(int Index);
void __fastcall SetDataItem(int Index, TDataCollectionItem
*Value);
public:
__fastcall TDataCollection(TComponent* Owner);
HIDESBASE TDataCollectionItem* __fastcall Add();
__property TDataCollectionItem* DataItem[int Index] =
{read=GetDataItem, write=SetDataItem};
};
__fastcall TDataCollection::TDataCollection(TComponent* Owner)
: TOwnedCollection(Owner, __classid(TDataCollectionItem))
{
}
TDataCollectionItem* __fastcall TDataCollection::Add()
{
return static_cast<TDataCollectionItem*>(inherited::Add());
}
TDataCollectionItem* __fastcall TDataCollection::GetDataItem(int
Index)
{
return
static_cast<TDataCollectionItem*>(inherited::GetItem(Index));
}
void __fastcall SetDataItem(int Index, TDataCollectionItem *Value)
{
inherited::SetItem(Index, Value);
}
| Quote: | ShowMessage("AnItem: " + String(AnItem->Index));
I get an error. ??
|
You did not say what the error actually is, but it is likely due to
you not overriding the TCollectionItem constructor correctly.
Gambit |
|
| Back to top |
|
 |
Maurice Anderson Guest
|
Posted: Sat May 05, 2007 8:11 am Post subject: Re: TCollection gives an error: why? |
|
|
| Quote: | class PACKAGE TDataCollection : public TOwnedCollection
|
I noticed you derived the class from TOwnedCollection instead of
TCollection, is that the way to do it? Or could I have derived it from
TCollection just as well with no problems?
| Quote: | void __fastcall SetDataItem(int Index, TDataCollectionItem *Value)
{
inherited::SetItem(Index, Value);
}
|
The inherited::SetItem(Index, Value) above gives an error that says
"Qualifier 'inherited' is not a class or namespace name". I figure its
because TOwnedCollection has no SetItem member. However, TCollection does
have a SetItem member function, should I consider deriving from TCollection
instead of TOwnedCollection?
Thanks |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon May 07, 2007 10:52 pm Post subject: Re: TCollection gives an error: why? |
|
|
"Maurice Anderson" <mauriceanderson (AT) hotmail (DOT) com> wrote in message
news:463c28be$1 (AT) newsgroups (DOT) borland.com...
| Quote: | I noticed you derived the class from TOwnedCollection instead
of TCollection, is that the way to do it?
|
I prefer to use TOwnedCollection because it handles GetOwner()
automatically. You can use TCollection directly if you want, as long
as you override GetOwner() yourself. In order to be editable at
design-time, the collection needs a valid Owner in order to be
streamed properly.
| Quote: | Or could I have derived it from TCollection just as well with no
problems? |
Yes, but only if you override GetOwner().
| Quote: | The inherited::SetItem(Index, Value) above gives an error that
says "Qualifier 'inherited' is not a class or namespace name".
|
You need to provide a typedef inside the class's declaration. Look at
the example I gave you earlier. It does exactly that:
typedef TOwnedCollection inherited;
| Quote: | I figure its because TOwnedCollection has no SetItem member.
|
Yes, it does. It is part of TCollection, which TOwnedCollection
derives from. Your error is because you did not declare the
'inherited' typedef itself like I showed you.
Gambit |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Fri May 11, 2007 8:11 am Post subject: Re: TCollection gives an error: why? |
|
|
"Maurice Anderson" <mauriceanderson (AT) hotmail (DOT) com> wrote in message
news:4643e815 (AT) newsgroups (DOT) borland.com...
| Quote: | when I destroy TDataCollection, are the items destroyed
automatically? |
Yes.
Gambit |
|
| Back to top |
|
 |
Maurice Anderson Guest
|
Posted: Fri May 11, 2007 8:11 am Post subject: Re: TCollection gives an error: why? |
|
|
| Quote: | Yes, it does. It is part of TCollection, which TOwnedCollection
derives from. Your error is because you did not declare the
'inherited' typedef itself like I showed you.
|
You are correct thanks.
On final question, when I destroy TDataCollection, are the items destroyed
automatically? |
|
| Back to top |
|
 |
|
|
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
|
|