10

I've been using visual studio for some time and it annoys me everytime when I work with Classes. The problem is, when I create an object of a Class I tend to see the private members belongs to that class and I don't want to, because what if I create a class with 10+ private variable, then it will be a nightmare, there must be a way to hide private members, If there is a way could you please share it with me? Thank you :)

EDIT:

Here is a picture that will help you understand what I'm talking about,

for example here I have 2 private variables of LinkedList class (curSize and head) I won't be able to alter them from main so, there is no point seeing them(is there?) How can I hide them without altering my code? is there a setting for that in Visual Studio?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Malkavian
  • 372
  • 1
  • 5
  • 16
  • Hard to see how that could be a real problem if you're talking about source code. Simply put the private parts at the end of the class. If you're actually talking about the debugger, well, it is its job to show the state of the object. – Hans Passant Jan 27 '12 at 00:18
  • Which language? if you are talking about C#, I know you can also use partial classes to break down a class members into several files. – 000 Jan 27 '12 at 00:18
  • If you are talking about the auto completion drop down, it follows the accessibility rules, so private members of an object of type A is accessible to any other object of type A. – 000 Jan 27 '12 at 00:21
  • @Sam I'm using C++ but I'm looking for a way to "not display" the private members, I don't want to alter my code I'm hoping there might be a way to do this by changing Visual Studio settings. – Malkavian Jan 27 '12 at 00:25
  • @Sam Yes I'm talking about the auto completion drop down, and my question is, is there a way to alter the accessibility rules? I bet C++ Standard also have private members, but when you use one of their class you don't see the private members. – Malkavian Jan 27 '12 at 00:50
  • 2
    That's a feature. It lists all the variables/methods you have access to in the current context you are in. If you are looking at code within an instance method, you are shown the variables in the current scope. That includes method parameters, local variables, instance variables, properties, methods, and so on. If you don't want to see that, then turn off intellisense. This is not an option you can configure. – Jeff Mercado Jan 27 '12 at 01:08
  • 1
    It seems that intellisense is showing members that are not accessible in the current context and he wants to avoid that. – David Rodríguez - dribeas Jan 27 '12 at 03:59

3 Answers3

6

This might not be the best answer nor is it a pretty answer but it get's the job done and if you can live with a small syntax change then it will definitely work. One trick that I learned from observing std classes such as std::vector is that they denote private members with the prefix _, thus forcing all the private members to the very bottom of intellisense. It doesn't remove them from the list but it will move them all to the very bottom so they don't bother you when you are scrolling the list. Here's an example:

class SomeClass{
public:
   int myPublicMemeber;
private:
   int _myPrivateMember;
};
idunnololz
  • 8,058
  • 5
  • 30
  • 46
  • wow nice trick, will definitely consider using it when I need to hide the private members Thank you for your answer! – Malkavian Nov 20 '12 at 04:42
4

You can use regions, like this:

class MyClass {

    #region Private Variables

    private int x;
    private int y;
    private int z;

    #endregion

}

Visual Studio will display a little - next to the #region line. Click it to hide the variables.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you for your Answer! :) but is there a way to do this without altering my source code? (like a Visual Studio setting to display/hide private members of a class) – Malkavian Jan 27 '12 at 00:28
  • @Malkavian I seriously doubt that there is one, because it is not easy to do: your private variables could be scattered through your code, so identifying collapsable regions automatically would be rather problematic. – Sergey Kalinichenko Jan 27 '12 at 01:56
  • You'd have to use `#pragma region` in C++, and that's not going to stop IntelliSense from showing the private members (but I realize you answered before the question was edited). – Cody Gray - on strike Jan 27 '12 at 03:27
  • @CodyGray Thank you for your answer:) I thought there might be a way to do this but thanks to dasblinkenlight I realized the problem was not as trivial as I thought. On the plus side this will force me to use least amount of private globals, and help me to use OOD smarter :) – Malkavian Jan 27 '12 at 05:03
4

Unfortunately, this is not possible in the current version of Visual Studio. In C++, the IntelliSense list is not filtered by accessibility or scope. Therefore, private members are still shown even where they are not actually accessible by your code. There are no settings to tweak this behavior, either.

You just have to rely on the lock icon to indicate that they're private and therefore inaccessible. All of those little icons in the IntelliSense window do have a meaning, you know.

But it looks like this feature might be coming in the next version of Visual Studio (VS11). MSDN says:

List Members Enhancements. The List Members drop-down appears automatically as you type code into the code editor. Results are filtered, so that only relevant members are displayed as you type. You can control the type of filtering logic used by the Member List in the Options dialog box under Text Editor, C/C++, Advanced.

As silly as it is, I'm rather excited about this, too. Along with other cool stuff like better syntax highlighting and reference highlighting. The Developer Preview is out already, so you could try to start using it if you want, but it may not be ready for prime time. And this is admittedly kind of a lousy reason to upgrade...

Alternatively, you could invest in Visual Assist X, which is an extension available for multiple versions of Visual Studio that adds a lot of convenience features to the C++ IDE and, pertinently, improves the IntelliSense filtering. It's not free, but it's pretty awesome for C++ developers, and if I wasn't poor/broke/cheap, I'd definitely buy it myself.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 1
    Nope, still didn't show up in VS 2012 RC. I guess we'll have to wait for Visual Studio 2014 before we get such a simple feature. If only the team weren't so busy ugly-ifying UI and stripping out all the color... – Cody Gray - on strike Jul 22 '12 at 13:11