Link to home
Start Free TrialLog in
Avatar of csharp_learner
csharp_learnerFlag for Singapore

asked on

Filtering of menus

Hi,

I'm working on VB6 and there is a sub function where i have encountered "procedure too large" error, reason is I have to filter the respective selections for individual Profile and it's location and it's hostname. In the end the whole code is a huge chunk of Case loops.

There are 3 variables to consider:
1. Profile (15 in total)
2. Location (35 in total)
3. PC hosts-name (35 in total)

And there are 6 menu selections:
1. Home
2. Control
3. Fire
4. Environment
5. Settings
6. Administration

Clicking on the any of the menu will pop up 5~6 selections.
The problem is the selections are filtered by the 3 variables hence the legacy code was something like...

Select Case Profile
      Case "Power"
              Select Case "Location"
                     Case "Home"
                              If hostsname like "abc" Then
                              ......
                              ......
                              ......



I hope experts can advice on how can i stream line the filtering process without over using Case,If Else loops.
ASKER CERTIFIED SOLUTION
Avatar of lojk
lojk
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of csharp_learner

ASKER

Thanks for your reply lojk,

Yes i agree it's managed in a bad way as it was a legacy code and i'm not so proficient in VB.

The code goes somthing like this...

    Case "Fire"
         Select Case Location
             Case Home
                      If HostName = "abc" Then            
                           Select Case Profile
                                 Case "Power", "user1", "user2", "user3", "main1", "main2"
             Menu.InsertMenuItemRegular 1, lLine, "Menu Selection 1", 100, Enabled
            Menu.InsertMenuItemSeperator 1, lLine
            Menu.InsertMenuItemSubMenu 1, lLine, "Menu Selection 2", 1000, Enabled, 2
            Menu.InsertMenuItemRegular 1, lLine, "Menu Selection 3", 101, Enabled
            Menu.InsertMenuItemRegular 1, lLine, "Menu Selection 4, 102, Enabled
            Menu.InsertMenuItemRegular 1, lLine, "Menu Selection 5", 103, Enabled
         
            lLine = 1
            Menu.InsertMenuItemRegular 2, lLine, "Sub Menu Selection 1", 104, Enabled
            Menu.InsertMenuItemRegular 2, lLine, "Sub Menu Selection 2", 105, Enabled

The first case is for the Power menu, second case is for location, the if statement is for the hostname and the last Case is for the profile. The executing of the menu selection is done in another sub function.

I have thought about writing each profile into a configuration file but there are alot of Profiles, locations and hostname to consider...
Yuk ;-(

You still havent really shown me enough code but to broaden my original answer, how about this..


  Case "Fire"
         Select Case Location
             Case Home
                      If HostName = "abc" Then            
                           Select Case Profile
                                 Case "Power", "user1", "user2", "user3", "main1", "main2"
       DoMenus_Fire_Home_PowerUserandMain()
       ........

Public sub DoMenus_Fire_Home_PowerUserandMain()
      Menu.InsertMenuItemRegular 1, lLine, "Menu Selection 1", 100, Enabled
            Menu.InsertMenuItemSeperator 1, lLine
            Menu.InsertMenuItemSubMenu 1, lLine, "Menu Selection 2", 1000, Enabled, 2
            Menu.InsertMenuItemRegular 1, lLine, "Menu Selection 3", 101, Enabled
            Menu.InsertMenuItemRegular 1, lLine, "Menu Selection 4, 102, Enabled
            Menu.InsertMenuItemRegular 1, lLine, "Menu Selection 5", 103, Enabled
         
            lLine = 1
            Menu.InsertMenuItemRegular 2, lLine, "Sub Menu Selection 1", 104, Enabled
            Menu.InsertMenuItemRegular 2, lLine, "Sub Menu Selection 2", 105, Enabled
End Sub


As i said i would split the inner section of each case into seperate subs - this would at least allow the total sub size to reduce but there is no real substitute for just rewriting this mess I'm afraid..
Avatar of eemit
Try to use Collections e.g.:

colProfile - use Profile name as key e.g. "Power"
colLocation - use Location as key e.g. "Home"
colHostsname - use Hostsname as key e.g. "abc"

To add an item to a collection:
colProfile.Add item, key

To retrieve an item from a collection:
colProfile.Item(index)    'as Index use key string that was specified when the item was added
colProfile(index)              'since Item is the default method, you can use the shorthand syntax
lojk: I'll try to compile more of my code and send in later on. It's my company's property hence i really can't show the whole stuff here. I'm avioding to use the sub function method as I have tried it before and there are lots of parameters involved to be passed and passed back in the legacy code hence i gave up on that idea.

eemit: can you eloborate more on how can I use the collection method to lessen the code?

Thanks for the help thus far experts.
Hi csharp_learner,
You can at least have a separate sub for each of 6 menu selections.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.