1

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?

IncomePitbull
  • 302
  • 2
  • 4
  • 14

2 Answers2

1

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.

John Woo
  • 258,903
  • 69
  • 498
  • 492
  • 1
    I see, I appreciate your reply. Sometimes, we don't have questions regarding exact problems in code, but rather what or why to better understand the language and programming concepts. You can make the 2nd example available in other classes, however I did some more searching, and realized what you mean about adding calculations to the 1st example. Last question, is the 1st example threadsafe? I normally use multi-threading for almost every app I make. – IncomePitbull May 23 '12 at 05:17
1

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.

Community
  • 1
  • 1
Tim M.
  • 53,671
  • 14
  • 120
  • 163
  • 1
    Thank you also for taking the time to reply, and linking to a helpful post. I read the first part of it, and I am about to read the entire page. It has a lot of detailed info, so great link you shared. – IncomePitbull May 23 '12 at 05:21
  • I voted this as the answer due to the more elaborate answer, and linking to two valuable articles so that I can fully understand. I appreciate both of you guys' replies, however only 1 can be selected :) – IncomePitbull May 23 '12 at 05:42
  • Thanks man for sharing the love :) One last question, and I don't feel it is needed to create a new question for it. Is the GET SET threadsafe? Or what would be your recommended ideal way to change the value (whether integer or string) of the SET or retrieve the GET. For example, I am on a background thread, and I want to change increase the value by 1 every time the foreach loops? I know of a few ways to do it, but I taught myself everything that I do know, so trying to get some other peoples ideas as well to expand myself. – IncomePitbull May 23 '12 at 05:55
  • That's a good question. In short, get/set is not inherently thread safe. Start with: http://stackoverflow.com/questions/505515/c-sharp-thread-safety-with-get-set. For increment operations, look at the `Interlocked` class. BTW, you will probably find more info about c# when you search for answers and 99% of the info is accurate for VB.Net. – Tim M. May 23 '12 at 05:59