Always use Public Properties. Follow on of the tenants of OOP.
A property allows you to not only get the value but then make sure that it
is a good one before using it.. Consider the case below..
Public Class Foo
Public mMessage As String'cannot be NULL
Public Sub PrintMessage()
Debug.WriteLine(mMessage)
End Sub
End Class
This method allows the caller to try to display a null value. You have no
control over the value at this point.
Also consider what might happen here if the message was printing and someone
changed the value at the same time. using a property allows you to check for
conditions like that.
Now consider the code below:
Public Class Foo
Private mMessage As String 'cannot be NULL
Private mBusy As Boolean = False
Public Property Message() As String
Get
Return mMessage
End Get
Set (value As String)
If mBusy = False) Then
If (not value Is Nothing) then
mMessage = value
Else
Throw New ArgumentNullException("value", "The value
cannot be null!.")
End If
Else
Throw New ArgumentException("The message value is in use!
Try again latter.")
End If
End Set
End Property
Public Sub PrintMessage()
mBusy = True
Debug.WriteLine(mMessage)
mBusy = False
End Sub
End Class
This ensures that the value provided can never be null. Always do your best
to keep data internal to the class insulated form out side of the class this
way. It's called encapsulation.
"Agnes" <ag***@dynamictech.com.hk> wrote in message
news:O8**************@TK2MSFTNGP14.phx.gbl...
In a single form , I can delcare a public vairable or property.
So. What is the difference between it ?? which is better ??
Dim frmTest as myForm
frmTest.strPublicVariable = "ABC"
frmTest.strPropertyVarialbe = "ABC"
Sorry for my stupid question .