Page 1 of 1

When to use #PB_Any instead of enumerated IDs

Posted: Wed Aug 17, 2022 4:59 pm
by vividpixel
Example 1 - Fails

Code: Select all

    menu_button_about = MenuItem(#PB_Any, "&About" + Chr(9) + "Shortcut")
Later in the code...

Code: Select all

    Case menu_button_about
      MessageRequester("About " + #Program_Title, 
                       "About Text", #PB_MessageRequester_Info)
Fails to display the MessageRequester, which I'm thinking is due to it not being in scope inside the Case statement? I tried the Shared keyword which works before the Case statement but not inside.

Example 2 - Succeeds
Using the typical enumeration works just fine, and I don't have a reason I can't do that.

Code: Select all

    MenuItem(#Menu_Button_About, "&About" + Chr(9) + "Shortcut")

Code: Select all

    Case #Menu_Button_About
      MessageRequester("About " + #Program_Title, 
                       "About Text", #PB_MessageRequester_Info)
However, I thought dynamic IDs would be convenient and I read some stuff about preventing ID conflict, so it seemed worth trying. But it does mean replacing an enumeration with tons of variables. What's the best practice?

Re: When to use #PB_Any instead of enumerated IDs

Posted: Wed Aug 17, 2022 5:12 pm
by STARGÅTE
Please read the documentation: MenuItem()

The MenuItemID has to be a number between 0 and 65535. #PB_Any is not valid here and do not generate a dynamic item id.

Re: When to use #PB_Any instead of enumerated IDs

Posted: Wed Aug 17, 2022 5:17 pm
by vividpixel
Thank you STARGÅTE, I had overlooked that upper limit from the docs.
Do you happen to know when it is appropriate to use #PB_Any? Docs show it in use for windows and the menu itself. I'm sure I will figure out over time where it works and doesn't, either way.

Re: When to use #PB_Any instead of enumerated IDs

Posted: Wed Aug 17, 2022 6:27 pm
by STARGÅTE
Windows and Menus can be created using #PB_Any, but not the menu item itself.
vividpixel wrote: Wed Aug 17, 2022 5:17 pm Do you happen to know when it is appropriate to use #PB_Any?
This question will be answered differently depending on who you ask.

Personally, I use static constants (Enumerate) whenever I create static content (Windows, Gadgets, Sprites, Images, ...).
It's easy to use, you need no additional variables, and you can group your constants with Enumerate <Name>.
Whenever I create a (public) include or a module I switch to #PB_Any, to prevent number overlaps.
Also dynamically created contents should use #PB_Any instead of iterating a number.