Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to hide a field (or just manipulate/hide from autocomplete) within its own class?

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?

like image 716
Wil Avatar asked Feb 28 '11 19:02

Wil


3 Answers

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
like image 95
PeterM Avatar answered Sep 28 '22 02:09

PeterM


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; }
like image 21
KBoek Avatar answered Sep 28 '22 03:09

KBoek


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.

like image 30
codemouse Avatar answered Sep 28 '22 04:09

codemouse