Re: Public variable VS property ?



"Agnes" <agnes@xxxxxxxxxxxxxxxxxx> wrote in message
news:O8$tr3hRFHA.1236@xxxxxxxxxxxxxxxxxxxxxxx
> 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.


.