Vb.net How do I hide this variable from the rest of the class?

Public Class frmMain
    Private p_dlgAdd As frmAdd = Nothing
    Public ReadOnly Property _dlgAdd As frmAdd
        Get
            If p_dlgAdd Is Nothing Then
                p_dlgAdd = New frmAdd()
            End If
            Return p_dlgAdd
        End Get
    End Property

    Public Sub DoStuff()
       ''// Should not touch p_dlgAdd
    End Sub
End Class

      

For multiple types of objects, I would prefer to initialize them only when needed (sql connection, mainframe connections, large forms) as some users only use certain parts of the program (managers can use one mainframe to do what they want, ordinary users are using another resource first).

Why p_? I think using p_ will help me not to use or easily find in intellisense a variable instead of a property locally in this class. Then, using _ yourself before private properties or private variables that don't require local access to the property.

What would be a good way to help me accidentally contact p_dlgAdd directly? Is this a good option for anonymous variables in 2008? (I don't have 2008 at work yet, but they think we'll be able to do it soon)

+1


a source to share


4 answers


You can use EditorBrowseableAttribute to hide a class member from Intellisense.

<EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)> _
Private p_dlgAdd As frmAdd = Nothing

      



Another option that comes to mind is to use a static analysis tool like Gendarme to check for violations of the "do not access" field directly "rule. Not sure if the gendarme already has a corresponding rule but it is extensible (in some cases, it may not be worth the effort).

+2


a source


To be honest, I am not familiar with anonymous variables. With that said, there really isn't a mechanism I know to stop you from writing code to access a private variable from the same class. Just pay attention to your code. If you want to put some kind of prefix in this variable to remind yourself, that's okay.



0


a source


What you can always do (but that might be overkill in your case) would be:

public class Base
{
    private frmAdd p_dlgAdd = null;
    protected frmAdd _dlgAdd 
    {
        get
        {
            if(p_dlgAdd == null)
                p_dlgAdd = new frmAdd();
            return p_dlgAdd;
        }
    }
}

      

This way your "p_dlgAdd" will not be available and _dlgAdd only in the derived class. But this only works if you can define / change the base type.

0


a source


For this, Static is used .

Public Class frmMain
    Public ReadOnly Property _dlgAdd As frmAdd
        Get
            Static dlgAdd As frmAdd = Nothing
            If dlgAdd Is Nothing Then
                dlgAdd = New frmAdd()
            End If
            Return dlgAdd
        End Get
    End Property

    Public Sub DoStuff()
       ' cannot touch dlgAdd
    End Sub
End Class

      

https://msdn.microsoft.com/en-us/library/z2cty7t8.aspx

0


a source







All Articles