2017-04-09 32 views
0

我有一个函数,当我打电话时,它让我不用(),为什么?以同样的方式调用函数和属性?

Public Class Socio 

Private _antiguedad As Integer 

Public Function RetornarAntiguedad() As Integer 
     Return _antiguedad 
    End Function 
End Class 

Sub Main() 
    Dim test = New Socio() 
    test.RetornarAntiguedad <--- This works even though it doesnt have(), why? 
    Console.ReadKey() 
End Sub 

它看起来像一个属性?它不会在函数和属性之间产生混淆?

回答

0

在VB语法中,函数调用在捕获其返回值时不需要尾随()

在上面的示例中,您将像调用方法一样调用该函数,而不捕获其返回值。因此Visual Studio IDE应该自动附加括号。

但是,如果你把它改成这样:

Dim test As New Socio() 
Dim result as Integer = test.RetornarAntiguedad 

...括号不再需要。

这只是VB.NET的语法,就是这样。完全不用担心。你的代码仍然可以工作。

1

括号是可选的,编译器因此不会返回错误,除非您正在添加参数。我认为这是由于VB.Net的“灵活”设计。