Is it possible to hide a field (or just manipulate

2019-06-21 18:30发布

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?

3条回答
再贱就再见
2楼-- · 2019-06-21 19:04

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楼-- · 2019-06-21 19:05

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
查看更多
Anthone
4楼-- · 2019-06-21 19:13

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.

查看更多
登录 后发表回答