5

I have a (quite) complicated file with the usual mix of components.

I have a field (called keyloaded) and a linked property (called Keyloaded).

Whilst working within the class, I accidentally directly manipulated the field instead of the property.

It most likely is because I am still quite a bit new to all of this (I triple check now!), however, this is already a private field and outside of working with the class works great. Is there just something simple I can do that will remove it from Autocomplete?

And if not, what are the best practices for a similar situation?

Whilst writing this question, I suddenly remembered in my book, they said about underscores... Is this just the best solution - to put it out of sight?

Wil
  • 10,234
  • 12
  • 54
  • 81

3 Answers3

6

Like Brian and KBoek mentioned you can just start your fields with an underscore. But if you really want to hide a method/field/property you can set the attribute as shown. This will prevent the method/field/property from being shown in intellesense. However, The member will still be accessible.

<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)> _
Public Property HiddenProperty()
    Get
        return _hiddenProperty
    End Get
    Set (value as object)
        _hiddenProperty = value
    End Set
End Sub
PeterM
  • 2,372
  • 1
  • 26
  • 35
  • Very interesting, +1 However, It is the field, not the property I want to hide. Does this change anything with the compile/output or is this strictly only within VS/the editor? – Wil Feb 28 '11 at 19:49
  • You should be able to add an atribute to the field as well. It won't change the compilation at all, it is strictly in the editor. – PeterM Feb 28 '11 at 19:50
2

I use underscore as a prefix to private fields, like "_keyloaded". If the property only sets and gets the field, consider creating an auto-property like this:

public bool Keyloaded { get; set; }
KBoek
  • 5,794
  • 5
  • 32
  • 49
  • It does more than that - so auto-properties are out, but thanks. for the naming convention. – Wil Feb 28 '11 at 19:35
1

I think Microsoft's current Code Syntax standards say that either Fields Or Properties can be Pascal Case. However, I have always stuck with the convention that fields should begin with an underscore. Change keyloaded to _keyloaded. I think you will find it much easier to identify the difference between fields, Properties, and locals this way.

codemouse
  • 13
  • 4
  • Also see here: http://stackoverflow.com/questions/450238/to-underscore-or-to-not-to-underscore-that-is-the-question – codemouse Feb 28 '11 at 19:33
  • It also helps to eliminate confusion between Case-related issues of field/Properties with the same name. I also agree with KBoek - you can create an auto-implemented Property. If you want to only set a public property within that class, then change the syntax to: public bool Keyloaded { get; private set; } – codemouse Feb 28 '11 at 19:43