I come from Java background, where we have data structures with interfaces that if its collection it support certain behaviour, and a set has another.
while programming in Delphi I thed to hit a brick wall when it comes in asking the reflection about the behaviour of items, its very strange. for example this code does not compile
menuOfSomeKind.Items.Add(t);
where menu of some kind is a component that has Items that contain other sub components, that are the menu entries.
if I want to dynamically edit this, meaning using the add behaviour, it says '[' expected but '.' foun开发者_如何学God.
Could you please clarify this?
Probably menuOfSomeKind
is TMenuItem
and not TMainMenu
If you are adding an item to TMenuItem
use MenuItem.Add(t);
If you are adding an item to TMainMenu
use MainMenu.Items.Add(t);
There's a difference between TMainMenu/TMenu and TMenuItem.
var
mainMenu: TMainMenu;
menu: TMenu;
subMenu: TMenuItem;
begin
//***** This creates a new root menu
mainMenu.Items.Add(TMenuItem.Create);
//***** Essentially the same as mainMenu
menu.Items.Add(TMenuItem.Create)
//***** This adds a new submenu to an existing menu
subMenu.Add(TMenuItem.Create);
//***** This adds a new submenu to the first submenu of an existing menu
subMenu.Items[0].Add(TMenuItem.Create);
end;
Note that presented code compiles but will throw exceptions all over the place when run. For starters, none of the local variables are assigned.
精彩评论