Board index » delphi » Interfaces&Objects creation assignment compatible but not cast assignment compatible

Interfaces&Objects creation assignment compatible but not cast assignment compatible

Why is it that if i have a variable of an interface, i can assign as object
to that variable when creating the object. But i cannot take an existing
object (of the same type) and make the assignment?

Given the declarations below, why does the first line work - but not the
second?

Sample Code
==========
  Generic := TMyForm.Create(nil);    //works, and delphi help gives example

   MyForm := TMyForm.Create(nil);
  Generic := MyForm   //this doesn't work???

Declarations
=========
var
    Generic: IGenericMethods;
    MyForm: TMyForm;

IGenericMethods = interface
    procedure FinagleTheGlaving;
end;

TMyForm = class(TForm, IGenericMethods)
public
    procedure FinagleTheGlaving;
end;

My basic objection is that the constructor returns in instance of an object.
But then again - so does the variable. Isn't it true that:
    TMyForm.Create(nil) is of type TMyForm  and
    MyForm is of type TMyForm

Why is it okay to assign to my interface variable something which is of type
TMyForm, but not something of type TMyForm?

And i hope it's a reason that my obviously low mental capacity can
understand.

Ian

 

Re:Interfaces&Objects creation assignment compatible but not cast assignment compatible


Quote
In article <3d20512a$1_1@dnews>, Ian Boyd wrote:
> Why is it that if i have a variable of an interface, i can assign as object
> to that variable when creating the object. But i cannot take an existing
> object (of the same type) and make the assignment?

> Given the declarations below, why does the first line work - but not the
> second?

Compiler magic. In my opinion the first statement should never have been
allowed but its too late to gripe now <g>.

Quote

> Sample Code
> ==========
>   Generic := TMyForm.Create(nil);    //works, and delphi help gives example

>    MyForm := TMyForm.Create(nil);
>   Generic := MyForm   //this doesn't work???

Try this:

  Generic := myform As IGeneric;

This requires the interface to have a GUID, though.

--
Peter Below (TeamB)  
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

Other Threads