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?
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.
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.
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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With