Solved

Filtering of menus

Posted on 2012-03-10
8
315 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:csharp_learner
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 9

Accepted Solution

by:
lojk earned 500 total points
ID: 37706607
The simplest way to reduce the size of the individual sub is to at least split the inner cases to seperate subs and pass all required params to those.

Select Case Profile
      Case "Power"
              DoPowerOptions(Param1, Param2 )
      Case "SomethingElse"

...


Public Sub DoPowerOptions (Param1 as string, Param2 as string)
           Select Case Param1  
                     Case "Location"
                            DoLocationOptions (Param2)

Public Sub DoLocationOptions (ParamCheck as string)
......


Although this is little more than a sticking-plaster solution to what if fundamentally flawed code. You really should rewrite this code as just from here it looks horribly unmanageable.

For example what happens if you need to add another hostname? You recompile the code and republish it? Consider storing some of the options (and the operations required by that option) in a configuration file or database and reading that into your method and using that information to make the decision as to which operation should do what.

If you could post the whole or at least a more detailed section of the code and some more information about what you are actually trying to achieve i might be able to make more suggestions.
0
 

Author Comment

by:csharp_learner
ID: 37708348
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...
0
 
LVL 9

Expert Comment

by:lojk
ID: 37709154
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..
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 15

Expert Comment

by:eemit
ID: 37713856
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
0
 

Author Comment

by:csharp_learner
ID: 37728339
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.
0
 
LVL 15

Expert Comment

by:eemit
ID: 37737952
Hi csharp_learner,
You can at least have a separate sub for each of 6 menu selections.
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 37848034
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now