I am trying to create an object from a class. I have tried to make a very simple example. I am having trouble getting the code to compile.
When I try to compile the code:
namespace DSTest19;
interface
uses
System.Drawing,
System.Collections,
System.Collections.Generic,
System.Linq,
System.Windows.Forms,
System.ComponentModel;type
///
/// Summary description for MainForm.
///
MainForm = partial class(System.Windows.Forms.Form)
private
method button1_Click(sender: System.Object; e: System.EventArgs);
protected
method Dispose(disposing: Boolean); override;
public
constructor;
end;TMyObj = class(Object)
public
S:String;
end;TMyClass = class of TMyObj;
TMyObjA = class(TMyObj)
public
SofA :String;
end;TMyObjB = class(TMyObj)
public
SofB : String;
end;implementation
{$REGION Construction and Disposition}
constructor MainForm;
begin
//
// Required for Windows Form Designer support
//
InitializeComponent();//
// TODO: Add any constructor code after InitializeComponent call
//
end;method MainForm.Dispose(disposing: Boolean);
begin
if disposing then begin
if assigned(components) then
components.Dispose();// // TODO: Add custom disposition code here //
end;
inherited Dispose(disposing);
end;
{$ENDREGION}method MainForm.button1_Click(sender: System.Object; e: System.EventArgs);
var LMyObj :TMyObj;
LMyClass :TMyClass;begin
LMyClass:=TMyObjB;
LMyObj:=new LMyClass;
LMyObj.S:=‘Hello’;
(LMyObj as TMyObjB).SofB:=‘World’;
end;end.
I get the compiler error on the line LMyObj:=new LMyClass;:
Severity Code Description Project File Line Suppression State
Error (E62) Type mismatch, cannot assign “not nullable TMyClass” to “TMyObj” DSTest19 D:\Prism\DSTest19\DSTest19\DSTest19\Main.pas 80
I cannot see what is wrong.
I have tried to replace the line:
LMyClass:=TMyObjB;
with
LMyClass:=classOf(TMyObjB);
but it makes no difference.
If somebody could give me a pointer on how to achieve this, it would most appreciated.