Rob Kennedy wrote:

> heromed wrote:
> > procedure TForm1.Button1Click(Sender: TObject);
> > var Str : string;
> > begin
> >    Str := ComponentToString(Panel1);
> >    Panel2 := StringToComponent(Str); // the line with the error
> > end;
> >
> > When compiling, I get the following error:
> >
> >    "Incompatible types: 'TPanel' and 'TComponent'"
>
> It gives you that error for the same reason this would:
>
> var
>    panel: TPanel;
> begin
>    panel := TComponent.Create(nil);
> end;
>
> Here's a better example:
>
> function NewComponent: TComponent;
> begin
>    Result := TPanel.Create(nil);
> end;
>
> var
>    panel: TPanel;
> begin
>    panel := NewComponent;
> end;
>
> Do you recognize the problem now?
>
> The function's declared return type is TComponent. When Delphi sees you
> call that function, it cannot assume anything more about the return
> value except that it will be a TComponent. It might be a TPanel, since
> TPanel descends from TComponent, but it might also be a TEdit, or a
> TPopupMenu, or even a TFoo that nobody's even written yet.
>
> Now, we know that NewComponent will always return a TPanel, so if the
> compiler were sufficiently smart, it might realize that and accept the
> assignment to the Panel variable anyway. But the StringToComponent
> function makes *no* mention of what kind of object it might produce.
> That information isn't determined until run time when the function reads
> the contents of the string, so the compiler has no way of knowing
> whether it is safe to assign the return value to a TPanel variable.
>
> To tell the compiler to treat a value of one type as a value of a
> subtype, you can type-cast:
>
> panel := NewComponent as TPanel;
>
> If the returned component isn't really a TPanel, then you'll get an
> exception at run time.
>
> -- 
> Rob
>
>
> -----------------------------------------------------
> Home page: http://groups.yahoo.com/group/delphi-en/
> To unsubscribe: [EMAIL PROTECTED]
>
>
>
> SPONSORED LINKS
> C programming language 
> <http://groups.yahoo.com/gads?t=ms&k=C+programming+language&w1=C+programming+language&w2=Java+programming+language&w3=C+++programming+language&w4=Concept+of+programming+language&c=4&s=126&.sig=wfHwnNNuJ9PRdPKzWXnmJA>
>  
>       Java programming language 
> <http://groups.yahoo.com/gads?t=ms&k=Java+programming+language&w1=C+programming+language&w2=Java+programming+language&w3=C+++programming+language&w4=Concept+of+programming+language&c=4&s=126&.sig=eIq6WSkMZB-e3QTGyXydYA>
>  
>       C programming language 
> <http://groups.yahoo.com/gads?t=ms&k=C+++programming+language&w1=C+programming+language&w2=Java+programming+language&w3=C+++programming+language&w4=Concept+of+programming+language&c=4&s=126&.sig=aXkBBJrbB2_-_2wwbyfLoQ>
>  
>
> Concept of programming language 
> <http://groups.yahoo.com/gads?t=ms&k=Concept+of+programming+language&w1=C+programming+language&w2=Java+programming+language&w3=C+++programming+language&w4=Concept+of+programming+language&c=4&s=126&.sig=9Ug4n_hw8V4Ui50VPPq_xQ>
>  
>
>
>
> ------------------------------------------------------------------------
> YAHOO! GROUPS LINKS
>
>     *  Visit your group "delphi-en
>       <http://groups.yahoo.com/group/delphi-en>" on the web.
>        
>     *  To unsubscribe from this group, send an email to:
>        [EMAIL PROTECTED]
>       <mailto:[EMAIL PROTECTED]>
>        
>     *  Your use of Yahoo! Groups is subject to the Yahoo! Terms of
>       Service <http://docs.yahoo.com/info/terms/>.
>
>
> ------------------------------------------------------------------------
>
>------------------------------------------------------------------------
>
>No virus found in this incoming message.
>Checked by AVG Free Edition.
>Version: 7.1.362 / Virus Database: 267.12.8/161 - Release Date: 3/11/2005
>  
>
When I tried:
   Panel2 := (StringToComponent(Str)) as TPanel;
I get a Runtime error:
  "Class TPanel not found"

What am I doing wrong ?

Regards
 Bob Stanton
 


------------------------ Yahoo! Groups Sponsor --------------------~--> 
Fair play? Video games influencing politics. Click and talk back!
http://us.click.yahoo.com/T8sf5C/tzNLAA/TtwFAA/i7folB/TM
--------------------------------------------------------------------~-> 

-----------------------------------------------------
Home page: http://groups.yahoo.com/group/delphi-en/
To unsubscribe: [EMAIL PROTECTED] 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/delphi-en/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to