Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

vb.net

I am coming from the C# world to VB.NET and this puzzles me. Why are there 2 ways of doing the same thing? or is there some difference I am not aware of?

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 as opposed to the other?

like image 389
Denis Avatar asked Mar 15 '12 21:03

Denis


People also ask

What is the difference between properties and functions?

The main purpose of the property is to allow class to expose its private variables and representing the data portion of the class, function representing the actions. In the following example, RotbotName() is a property, it representing a conception thing, a data member. VerifyName() is a function.

What is parameterized function in VB net?

A parameter allows the calling code to pass a value to the procedure when it calls it. You declare each parameter for a procedure the same way you declare a variable, specifying its name and data type. You also specify the passing mechanism, and whether the parameter is optional.

What is the difference between parameters and properties?

You pass parameters to functions and constructors, and classes have properties. The constructor of the Person class in your example has a single parameter, and so does the double function. In this case, the firstName parameter is not a property!

What is a property in Visual Basic?

A property is a value or characteristic held by a Visual Basic object, such as Caption or Fore Color. Properties can be set at design time by using the Properties window or at run time by using statements in the program code. Object. Property = Value.


1 Answers

There's plenty of history behind this question, this goes back to 1997 when Microsoft released the COM Automation specification. Which allowed property setters/getters to have arguments. Visual Basic was an early adopter of the spec, it was driven in no small part by the language to find a replacement for the VBX extension model. Which ran out of gas around that time, it was heavily dependent on the 16-bit coding model.

The C# team took a pretty no-nonsense attitude to the feature, they absolute hate syntax ambiguities. That just doesn't belong in a brand new language. VB.NET didn't have the same luxury, they had to at least support some of the features of the previous generation, VB6 at the time.

Zip forward 10 years, the C# team had to back-pedal a bit by popular demand. Indexed properties are rife in, for example, the Office object model. In C# version 4 they allowed indexed properties exclusively for COM interfaces to soften the pain of writing C# Office code. And more, optional and named arguments got added as well to deal with the Type.Missing misery. And the dynamic keyword to support late binding, another important feature of COM and Visual Basic that was really painful to do in C# without that keyword.

Long story short, COM is beautiful, the elegance of IUnknown is stark. Tony Williams is the genius behind it. Video is here, much worth watching. The subset of COM Automation, IDispatch, is not so beautiful. But it was incredibly successful. Languages ignore it at their peril. C# didn't.

These details might sound arcane from an era long gone but they are not. The next version of the Windows API, WinRT is completely based on IUnknown. Otherwise known as "Metro" or "Modern UI". IDispatch did not survive, replaced by IInspectable.

like image 77
Hans Passant Avatar answered Oct 12 '22 00:10

Hans Passant