 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Paul Nicholls Guest
|
Posted: Thu Jul 10, 2003 12:30 am Post subject: Incompatible types ISerializableObject and TObject... |
|
|
Hi all, I have defined this type
TObjectClass = class of TObject;
I have an array
var
RegisteredClasses: array of record
ClassID: AnsiString;
RegisteredClass: TObjectClass;
end;
and created these two routines
procedure RegisterClassByClassID(ClassID: AnsiString; const RegisteredClass:
TObjectClass);
var
i: Integer;
ClassIDFound: Boolean;
begin
ClassIDFound := False;
for i := 0 to High(RegisteredClasses) do
if(RegisteredClasses[i].ClassID = ClassID)then
begin
ClassIDFound := True;
Break;
end;
if(ClassIDFound)then
raise Exception.Create('RegisterSerializableClass: Class with ClassID
"'+ClassID+'" already registered.')
else
begin
SetLength(RegisteredClasses,High(RegisteredClasses)+2);
RegisteredClasses[High(RegisteredClasses)].ClassID := ClassID;
RegisteredClasses[High(RegisteredClasses)].RegisteredClass :=
RegisteredClass;
end;
end;
function CreateNewObjectFromClassID(ClassID: AnsiString): TObject;
var
i: Integer;
begin
Result := nil;
for i := 0 to High(RegisteredClasses) do
if(RegisteredClasses[i].ClassID = ClassID)then
begin
Result := RegisteredClasses[i].RegisteredClass.Create;
Break;
end;
end;
These are used like so:
RegisterObjectByClassID('SomeClassID',TMyClass);
I can then get back any registered object using the class id as the key eg.
(returns nil if not found). (works nicely)
NewObject := CreateNewObjectFromClassID('SomeClassID');
This work very nicely for 'normal' classes, but I have a problem when I
store classes that 'inherit' interfaces, eg.
Lets say I have an interface
ISerializableObject = interface
['{A8CE6CD8-2246-488E-A292-65F7C56F6360}']
procedure UnSerialize(var s: AnsiString);
function Serialize: AnsiString;
function ToString: AnsiString;
end;
and a class using this
TSerializableClass = class(TInterfacedObject,ISerializableObject)
private
{...}
end;
I do this
var
SerializableObject: ISerializableObject;
begin
RegisterClassByClassID('TestID',TSerializableObject);
SerializableObject := CreateNewObjectFromClassID('TestID');
Even though TSerializableClass is stored in the RegisteredClasses array and
inherits the interface, the
compiler complains with
Incompatible types: 'ISerializableObject' and 'TObject'
Is there anyway I can get around this?
Thanks in advance,
Paul Nicholls (Delphi 5/6 Professional)
"The simple things in life are often a pest..." - Chris Valentine
[email]paul-nicholls (AT) hotmail (DOT) com[/email]
Replace '-' with '_' to reply
|
|
| Back to top |
|
 |
Sebastian Moleski Guest
|
Posted: Thu Jul 10, 2003 1:04 am Post subject: Re: Incompatible types ISerializableObject and TObject... |
|
|
"Paul Nicholls" <paul-nicholls (AT) hotmail (DOT) com> wrote
| Quote: | var
SerializableObject: ISerializableObject;
begin
RegisterClassByClassID('TestID',TSerializableObject);
SerializableObject := CreateNewObjectFromClassID('TestID');
Even though TSerializableClass is stored in the RegisteredClasses array and
inherits the interface, the
compiler complains with
|
How is the compiler supposed to know that the object that
CreateNewObjectFromClassIID returns implements your interface? It can't do so
statically which is why you get that compiler error. One way to get around that
is to use Supports or TObject's GetInterface function.
sm
|
|
| Back to top |
|
 |
Paul Nicholls Guest
|
Posted: Thu Jul 10, 2003 1:16 am Post subject: Re: Incompatible types ISerializableObject and TObject... |
|
|
Have you an example on how to do this?
Thanks...
"Sebastian Moleski" <sebmol (AT) yahoo (DOT) com> wrote
| Quote: | "Paul Nicholls" <paul-nicholls (AT) hotmail (DOT) com> wrote in message
news:3f0cb3ba$1 (AT) newsgroups (DOT) borland.com...
var
SerializableObject: ISerializableObject;
begin
RegisterClassByClassID('TestID',TSerializableObject);
SerializableObject := CreateNewObjectFromClassID('TestID');
Even though TSerializableClass is stored in the RegisteredClasses array
and
inherits the interface, the
compiler complains with
How is the compiler supposed to know that the object that
CreateNewObjectFromClassIID returns implements your interface? It can't do
so
statically which is why you get that compiler error. One way to get around
that
is to use Supports or TObject's GetInterface function.
sm
|
|
|
| Back to top |
|
 |
Ritchie Annand Guest
|
Posted: Thu Jul 10, 2003 8:56 am Post subject: Re: Incompatible types ISerializableObject and TObject... |
|
|
In article <3f0cb3ba$1 (AT) newsgroups (DOT) borland.com>, [email]paul-nicholls (AT) hotmail (DOT) com[/email]
says...
| Quote: | I do this
var
SerializableObject: ISerializableObject;
begin
RegisterClassByClassID('TestID',TSerializableObject);
SerializableObject := CreateNewObjectFromClassID('TestID');
Even though TSerializableClass is stored in the RegisteredClasses array and
inherits the interface, the
compiler complains with
Incompatible types: 'ISerializableObject' and 'TObject'
Is there anyway I can get around this?
|
CreateNewObjectFromClassID creates TObjects, and TObject doesn't
implement *any* interfaces It should complain the same way if you
tried to do:
var
AObject : TButton;
begin
RegisterClassByClassID('TestID',TSerializableObject);
AObject := CreateNewObjectFromClassID('TestID');
That's an "up-cast" from TObject to TButton, and Delphi won't do it
without explicit instruction. Same goes for interfaces.
What you want is to use the likes of GetInterface to try and pull an
interface out of the object...
var
SerializableObject: ISerializableObject;
begin
RegisterClassByClassID('TestID',TSerializableObject);
if CreateNewObjectFromClassID('TestID').GetInterface(
ISerializableObject,SerializableObject) then
begin
// here, you can use SerializableObject
end;
end;
Supports does nearly the same thing, except it involves a GetInterface to
IUnknown first.
Both work fine for getting interfaces from objects that DO implement
IUnknown (TInterfacedObject is one , but Supports will not work with,
say, TComponents unless you specifically declare support for IUnknown in
the class.
The one advantage of Supports is that it will return False if the object
is nil, whereas, of course, GetInterface will return an access violation
:)
Hope that helps! :)
-- Ritchie Annand
Senior Software Architect
Malibu Software & Engineering Ltd.
Business: http://www.malibugroup.com
Personal: http://nimble.nimblebrain.net
Wiki: http://wiki.nimblebrain.net
|
|
| Back to top |
|
 |
Paul Nicholls Guest
|
Posted: Thu Jul 10, 2003 10:57 pm Post subject: Re: Incompatible types ISerializableObject and TObject... |
|
|
Thanks for the info Ritchie, it does help :)
"Ritchie Annand" <nimble_minusthisbit (AT) shaw (DOT) ca> wrote
| Quote: | In article <3f0cb3ba$1 (AT) newsgroups (DOT) borland.com>, [email]paul-nicholls (AT) hotmail (DOT) com[/email]
says...
I do this
var
SerializableObject: ISerializableObject;
begin
RegisterClassByClassID('TestID',TSerializableObject);
SerializableObject := CreateNewObjectFromClassID('TestID');
Even though TSerializableClass is stored in the RegisteredClasses array
and
inherits the interface, the
compiler complains with
Incompatible types: 'ISerializableObject' and 'TObject'
Is there anyway I can get around this?
CreateNewObjectFromClassID creates TObjects, and TObject doesn't
implement *any* interfaces It should complain the same way if you
tried to do:
SNIP |
|
|
| 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
|
|