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 ------------------------ Yahoo! Groups Sponsor --------------------~--> Get Bzzzy! (real tools to help you find a job). Welcome to the Sweet Life. http://us.click.yahoo.com/A77XvD/vlQLAA/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/

