how to detect if a variable exists or not ?

hi everyone,

lets say I have some methods foo1,foo2,foon I wrote and I share among differents projects
in every project, I have a menubar, and some projects needs foo1, others need foo2, foon
If I want to use foox in a project, I just add a menuitem with the foox line and it calls the foox method
but how to detect if a foox menuitem has been added in the ide ?
because if there is no foox menuitem, I get errors at compile time saying “the foox item does not exist”

how do you manage that “shareds methods among multiples projects thing” ?
is there a way to detect that a menu item is in the app or not ?
I dont want to have many greyed menu item lines, only if they are here then call the method.

thanks.

Loop over the menu items and check for their names:

For i As Integer = 0 To App.MenuBar.Count - 1 If App.MenuBar.Item(i).Name = "foo1" Then // found Exit For i ElseIf App.MenuBar.Item(i).Count > 0 Then // loop over the submenu items For j As Integer ... ... ... End Next

[quote=142109:@Eli Ott]Loop over the menu items and check for their names:

For i As Integer = 0 To App.MenuBar.Count - 1 If App.MenuBar.Item(i).Name = "foo1" Then // found Exit For i ElseIf App.MenuBar.Item(i).Count > 0 Then // loop over the submenu items For j As Integer ... ... ... End Next[/quote]

Your method would work if the code ran. But if a variable is missing, it simply does not compile, so there is no way to detect that from inside the program.

Hes has to get rid of all direct references. He can’t use foox, he has to get the menuitem by its name “foox”. Then it will compile.

I would dynamically construct the menubar item using the items needed for the particular app. Then in the code that executes when the menu item is selected, you can have a broader more universal Select Case statement that can include cases for selections that may or may not exist in a given program.

yes I was looking into that way that seems to me the more feasible ?
but it’s not really acceptable because you have a big select…case that needs to be updated each time you make a new foo method
it’s not “object friendly” in my opinion.

would be nice if the #if could be used with a variable, and also some #ifexists
like the #ifndef in C ?

It sounds like you have code in you methods / module that refers to the menu items by name ?
To make this work dynamically and NOT cause those compilation problems you need to do like Eli suggests and look them up by name or tag or something else.