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

泪湿孤枕 提交于 2019-12-23 10:58:15

问题


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?


回答1:


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



回答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; }



回答3:


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.



来源:https://stackoverflow.com/questions/5146607/is-it-possible-to-hide-a-field-or-just-manipulate-hide-from-autocomplete-withi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!