Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hide class module private statements from Object Browser

Status
Not open for further replies.

JTBorton

Technical User
Jun 9, 2008
345
DE
When I look at my class module in the object browser, it displays all of my private variables and functions. This would be VERY confusing and overwhelming for some poor coder who wants to pick up the class and use it. How can I hide this information from the object browser like all of the built in classes do?

-JTBorton
Another Day, Another Disaster
 
JTBorton,
Can't.

I don't remember if you couldn't hide things in VB 6 or if the functionality wasn't included in VBA.

There are a some VB 6 [tt]Attribute[/tt]'s that you might find useful if your doing class work.

Try searching the internet for [tt]VB_UserMemId[/tt] if you want to see an example.

Not much help,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
This happens only for open vba project. Lock it for viewing with password to hide out of scope items.

combo
 
combo Thanks. I guess I could make it an add-in, but want them to be able to view to code in case they are not sure how to use incorporate it.

CautionMP So I have done a bit of digging, but all I can come up with is

VB_UserMemId = 0 makes a property the default property
VB_UserMemID = -4 makes an Enum the default Emum (why would you want to do this?)
VB_UserMemId = 40 is supposed to make the property/Enum invisible but doesn't really work in VBA.

Everything else I have come across is irrelevant or not helpful. I spent some time digging through my Visual Studio 2005 object library, but I had no luck finding it. So I still do not understand what exactly it is or what exactly it does. Think you can point me in the right direction? What other little jewels are there to dig up? I'm very interested to learn.

-JTBorton
Another Day, Another Disaster
 
No need to hide workbook, just lock vba project (otherwise there is no purpose to hide anything). This has nothing to do with workbook protection or converting to the add-in.

To lock the vba project, in vbe:
- select vbproject
- Tools>VBAProject properties... (or other vbproject name if was changed),
- 'Protection' tab,
- 'Lock project for viewing' + password.

Please note that protected project will stay closed in opened file, however it can execute its code. It is similar to design-time and run-time states.

The project, if opened, exposes all variables in object browser. Private ones are hidden if the project stays closed. As long as you do not share the password to open the project, the user will see only public variables, properties, methods or events in the object browser and their descriptions if added.

combo
 
JTBorton,
I use both the default and default enum settings to keep my own classes consistent with other built in classes, so I think it's worth the extra steps needed to incorporate into custom classes. Here are some examples of how/where they can be used.

Default property: Good example is the [tt]Excel.Worksheets[/tt] class. The default property is [tt]_Default[/tt], this allows you to grab a worksheet using [tt]Worksheets("Sheet1")[/tt] in leiu of using the fully qualified [tt]Worksheets.Item("Sheet1")[/tt]

Default Enum: Allows you to do a [tt]For...Each[/tt] loop on your class (I do a lot of collection classes) instead of doing a [tt]For...Next[/tt] loop.
Code:
For Each [i]Worksheet[/i] In ThisWorkbook.Worksheets
[tab]'Do somenting
Next [i]Worksheet[/i]

You won't find them in .Net, these settings are now stored in the .proj file and not in the class itself, to find the VBA stuff you have to go back to Visual Basic 6.

You don't have to make them add-ins to share. From the IDE you can go to Tools > References, then browse to the Excel file that has the code you want to share just like you would another library. Just be sure any users of a workbook built this way have access to the file you have as a reference.

If your going to follow combo's advice and 'Lock the Project for viewing' you will need to set the instancing for you class modules to PublicNotCreatable or else the class will be completely hidden in the Object Browser.

CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
CautionMP
Default property: Good example is the Excel.Worksheets class. The default property is _Default, this allows you to grab a worksheet using Worksheets("Sheet1") in leiu of using the fully qualified Worksheets.Item("Sheet1")

Default Enum: Allows you to do a For...Each loop on your class (I do a lot of collection classes) instead of doing a For...Next loop.
Code:
For Each Worksheet In ThisWorkbook.Worksheets
    'Do somenting
Next Worksheet

Yes I read a little about this on some of the web pages I dug up. But perhaps you can help me understand a little more how this works -

As I understand it, when you set the default property of an object, for example the Worksheets.Item, it means you can simply type Worksheets = "Sheet1". How do you code it so that instead you could simply type Worksheets("Sheet1")? Is it because the Item is an array of subclasses the worksheet class?

Likewise, and even more confusing, with the default Enum. I thought enums were simply constant Strings that represent Long values. How do they enable you to do a For Each loop?

-JTBorton
If it isn't broken, it doesn't have enough parts yet.
 
The word 'enum' is short for two slightly different things

1. enum: a distinct type consisting
of a set of named constants known as the enumerator list
2. enum: a function that enumerates ...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top