When to use #PB_Any instead of enumerated IDs

Just starting out? Need help? Post your questions and find answers here.
User avatar
vividpixel
New User
New User
Posts: 7
Joined: Tue Aug 09, 2022 9:36 am
Location: United States

When to use #PB_Any instead of enumerated IDs

Post 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?
Coding on MacOS 13, Windows 11 Pro under VMWare Fusion.
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: When to use #PB_Any instead of enumerated IDs

Post 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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
vividpixel
New User
New User
Posts: 7
Joined: Tue Aug 09, 2022 9:36 am
Location: United States

Re: When to use #PB_Any instead of enumerated IDs

Post 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.
Coding on MacOS 13, Windows 11 Pro under VMWare Fusion.
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: When to use #PB_Any instead of enumerated IDs

Post 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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Post Reply