jecarfor
jecarfor

Reputation: 529

Vb.Net Properties Syntax

I have asked my colleagues at work and even tried to look up this on the internet but I haven't been able to get an answer.

What is the difference between

Public Property Name As String

and

Public Property Name() As String

What difference makes adding () after the property name?

Upvotes: 8

Views: 2005

Answers (3)

Ian Horwill
Ian Horwill

Reputation: 3025

According to the online language reference here, the parens are required:

Property name ( [ parameterlist ] )

though as we know they can be omitted with no change to the meaning, if there are no parameters.

However there's an important difference when referencing the property. If you have an overloaded property and an overload with parameters references one without, the parens are mandatory on the call to the parameterless overload otherwise it resolves as a call to self which returns nothing.

I.e. in the following code, there is an "uninitialized" warning on line 17: Return MyProp.ToUpper() and it generates a null reference exception at runtime.

If you add parens to the two "recursive" calls, i.e. MyProp(), it works as expected.

Class Class1

Public Shared Sub Main()
    Dim c As New Class1
    Console.WriteLine(c.MyProp(upper:=True))
End Sub

Public Sub New()
    MyProp = "lower"
End Sub

Public ReadOnly Property MyProp As String

Public ReadOnly Property MyProp(upper As Boolean) As String
    Get
        If upper Then
            Return MyProp.ToUpper()
        Else
            Return MyProp
        End If
    End Get
End Property

End Class

Upvotes: -1

Hans Passant
Hans Passant

Reputation: 941465

You lookup these kind of details in the VB.NET Language Specification. It is a pretty formal document but nevertheless quite readable. Chapter 9.7 contains all the details about the Property keyword syntax. You'll for example see:

PropertySignature  ::=
    Property  Identifier  [  OpenParenthesis  [  ParameterList  ]  CloseParenthesis  ]
        [  As  [  Attributes  ]  TypeName  ]

The [] brackets indicate optional parts of the syntax. So you can easily see that you don't have to use () if the property doesn't take any parameters. In other words, when it is not an indexed property.

So there is no difference.

Upvotes: 4

Vahid Farahmandian
Vahid Farahmandian

Reputation: 6568

First of all you may find it that Property has many similarities to Methods. from this prospective, parenthesis in Property used for parameters. if a Property has no parameter you can omit it. following is the full property declaration syntax:

[Default] [Modifiers] Property PropertyName[(ParameterList)] [As DataType]
[AccessLevel] Get
    ' Statements of the Get procedure.
    ' The following statement returns an expression as the property's value.
    Return Expression
End Get
[AccessLevel] Set[(ByVal NewValue As DataType)]
    ' Statements of the Set procedure.
    ' The following statement assigns newvalue as the property's value.
    LValue = NewValue
End Set
End Property

You may find valuable tips in following links: What is the difference between a parameterized property and function in vb.net? AND https://msdn.microsoft.com/en-us/library/e8ae41a4.aspx

Upvotes: 6

Related Questions