Using a "class" in a TDictionary?

The idea is simple - create a TDictionary with a class name for TComponent to use

for enum in vm.ClassNameToComponent do TLuaClassTemplate<enum.Value>.RegisterClass(vm.LS, PrintGlobal, container, vm); 

listing instead

 TLuaClassTemplate<TButton>.RegisterClass(vm.LS, PrintGlobal, container, vm); TLuaClassTemplate<TPanel>.RegisterClass(vm.LS, PrintGlobal, container, vm); TLuaClassTemplate<TEdit>.RegisterClass(vm.LS, PrintGlobal, container, vm); ... 

and use the class name taken from xml to work with classes of a general type.
But there's a problem:

 TClassNameToComponentDict = TDictionary<string, TComponent>; ... ClassNameToComponent: TClassNameToComponentDict; ... ClassNameToComponent := TClassNameToComponentDict.Create; ClassNameToComponent.Add('TButton', TButton); ClassNameToComponent.Add('TPanel', TPanel); ClassNameToComponent.Add('TEdit', TEdit); ... 

error "Incompatible types" TComponent "and" TButton class "".
How to use a "class" such as TButton, etc. How is the general meaning?

+4
source share
1 answer

Type you used

 TDictionary<string, TComponent> 

represents a mapping from a string to an instance of a class. But you need to map the string to the class . Therefore you need to:

 TDictionary<string, TComponentClass> 

Where

 TComponentClass = class of TComponent 

Note: you do not need to declare a TComponentClass , as it is already declared in the Classes block.

+5
source

All Articles