Public variable VS property ?

  • Thread starter Thread starter Agnes
  • Start date Start date
A

Agnes

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 .
 
Agnes said:
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"

If it's a Property, you can have particular code execute when you set or get
the value, for instance making a calculation or validating the new value.
This can't happen when it's a public variable.


Daniel
 
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 said:
In a single form , I can delcare a public vairable or property.
So. What is the difference between it ??

There are no Stupid Questions - just the occasional Silly Answer
(of which this /isn't/ one, I hope) ...

A Property can be controlled, because you get to see every value
coming in or going out.

Compare these two.

Public ValueThatMUSTBeBetweenOneAndThree as Integer

If this value is used within your class to, say, index into a three-element
array and something in the Outside World does ...

YourObject.ValueThatMUSTBeBetweenOneAndThree = 7

.... your class is in trouble!

Contrast that with

Public Property VTMBBOneAndThree() as Integer
Get
Return m_iIndex
End Get
Set( ByVal Value as Integer )
Select Case Value
Case 0 to 2
m_iIndex = Value
Case Else
Throw New ArgumentException( ...
End Select
End Set
End Property
Private m_iIndex as Integer = 0

Now if something in the Outside World tries to do ...

YourObject.VTMBBOneAndThree = 7

.... /they're/ in for a nasty surprise, but /your/ object will remain
quite happy.

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top