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)
a source to share
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).
a source to share
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.
a source to share
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.
a source to share