Reputation: 292
I have been wondering for a while what the difference is between the following two:
Public Property ProgressMaxValue() As Integer
Get
Return maxval
End Get
Set(ByVal Value As Integer)
maxval = Value
End Set
End Property
vs
Dim progressMaxValue as Integer
ProgressMaxValue = 1184
The do the same exact thing right? I have examined other people's code, and am seeing more and more of the first example. Just trying to figure out the point, difference, and why people are using it lol. Maybe I missed the memo?
Upvotes: 1
Views: 676
Reputation: 54377
just declaring it as normal...
There is no "normal" in this case. The two statements are actually completely different things all the way down to the IL, i.e. a property and a field. When you use the getter or setter of a property via reading or assignment, you are actually invoking a method.
In your particular example, the getter/setter methods of the property read and update a field, but since they are methods, they could do anything that you wanted them to.
As to why, this has been discussed extensively, such as here and here (c# articles but interchangeable with VB.Net in this case). A broad (but good) justification for using properties is that it hides the internals of your class from external callers.
Upvotes: 1
Reputation: 263723
You are creating a Property
on your first code. Which will also allow you to expose it in other classes as long as it has been instantiated. The second one is only a variable available within the class or even the scope within the procedure only. There are differences within the two. You can also add a calculation in your property.
Upvotes: 1