What is the difference between a parameterized property and a function in vb.net?

I am coming from the C # world to VB.NET and it puzzles me. Why are there two ways to do the same? or is there any difference I don’t know about?

What is the difference between the following:

Public ReadOnly Property Test(ByVal v as String) As Integer Get Return SomeOperationOn(v) End Get End Property 

and

 Public Function Test(ByVal v as String) As Integer Return SomeOperationOn(v) End Function 

When do you use one and not the other?

+10
source share
3 answers

Functionally there is no difference, they both return a value based on the parameter. In fact, properties are actually converted to functions at compile time, since the concept of a property does not exist in MSIL .

Semantically , however, there is a difference in how they should be used. Properties are intended to reveal the internal state of an object. On the other hand, functions must work in the state of the object in order to either give an answer to a specific question (request), or in some way change the state (command).

Here is an example:

 Public Class Rectangle Private _size As Size ReadOnly Property Size() As Size Get Return _size End Get End Property Public Function IsSquare() As Boolean Return _size.Width = _size.Height End Function End Class 

While Size simply provides an object property, the IsSquare function actually performs an operation on the internal state of the object to answer the question.

Based on this principle, the most common use case for parameterized properties in VB.NET is a class representing a sequence of elements, where this parameter is used to access a specific element in a sequence by its position or by some unique key. In other words, to create what is known as indexers in C #.

+14
source

There is a lot of history behind this issue, this refers to 1997, when Microsoft released the COM Automation specification. Which allowed property developers / receivers to have arguments. Visual Basic was an early adopter of the specification; it was largely modified by the language to find a replacement for the VBX extension model. During this time, he had no gas; he depended heavily on the 16-bit coding model.

The C # team took a rather ridiculous attitude towards this function, they are incompatible with the synchronism of hate syntax. It just doesn’t apply to a completely new language. VB.NET didn’t have the same luxury; they had to at least support some functions of the previous generation, VB6 at that time.

Going forward 10 years, the C # team had to practice a bit on popular demand. Indexed properties are common, for example, in the Office object model. In C # 4, they allowed indexed properties exclusively for COM interfaces to alleviate the pain of writing C # Office code. And yet, additional and named arguments have also been added to deal with the sadness of Type.Missing. And a dynamic keyword to support late binding, another important COM and Visual Basic feature that was very painful in C # without this keyword.

In short, COM is beautiful, IUnknown's elegance is impeccable. Tony Williams is the genius behind him. The video here is worth a look. A subset of COM Automation, IDispatch, is not so pretty. But it was incredibly successful. Languages ​​ignore this at their own peril and risk. C # - no.

These details may sound secret from a bygone era, but it is not. The next version of the Windows API, WinRT is completely based on IUnknown. Otherwise, it is known as the Metro or Modern Interface. IDispatch not saved, replaced with IInpectable.

+13
source

The property may also have a setter:

 Public Property Test(ByVal v as String) As Integer Get Return SomeDictionary(v) End Get Set SomeDictionary(v) = Value End Set End Property 

It matters because it allows you to write something like this:

 MyObject.Test(index) = SomeValue 

C # only allows you to assign this through property indexers:

 MyOjbect[index] = SomeValue; 

This means that in C # you can only have one indexed property for each type. VB.Net allows you to use more than one indexed property for a type. To get the equivalent syntax, C # would either have to directly use the base dictionary, or if you have other code in getter / setter (e.g. logging), you will need to create an additional type for your dictionary.

+7
source

All Articles