Create instance from Classtype


(Friedrich Westermann) #1

I have these declarations:

type
TCwBaseTableClass = public class of TCwBaseTable;

TCwBaseTable = public class( ICwBaseTable) …

and a Dictionary<String, TCwBaseTableClass>;

method getICwTable(const aClassname: String; aBase: IDBcwDatabase): ICwBaseTable;
begin     
  if gTableClasses.ContainsKey(aClassname) then
  begin
  // if i Create a local var it works
    var lClass : TCwBaseTableClass;
    lClass := gTableClasses[aClassname];
   exit new lClass(aBase) as ICwBaseTable;
   // But
   // Why is these not working? 
   // I get error E130: Type expected
   // exit new gTableClasses[aClassname](aBase) as ICwBaseTable;
  end
  else exit nil;
end;

(Carlo Kok) #2

That’s because it’s an ambigious syntax; ie new Integer[15] vs new Something(). If you add parenthesis it probably works:

exit new (gTableClasses[aClassname])(aBase) as ICwBaseTable;


(Friedrich Westermann) #3

works! Thanks a lot!