.net 如何理解子项或集合?

.net 如何理解子项或集合?,.net,vb.net,visual-studio,.net,Vb.net,Visual Studio,我是VB.NET 2012(Visual Studio 2012)的新手 我想知道如何理解下面的错误消息 'Return' statement in a Sub or a Set cannot return a value. 重点是“子集合” 出于兴趣,我正在尝试通过多种方式打印消息。您必须在子部分或集合中有Return语句,并且您正在尝试返回值。你不能那样做 Sub Something Return 1 ' Error End Sub 如果需要返回值,则需要函数: Functio

我是VB.NET 2012(Visual Studio 2012)的新手

我想知道如何理解下面的错误消息

 'Return' statement in a Sub or a Set cannot return a value.
重点是“子集合”


出于兴趣,我正在尝试通过多种方式打印消息。

您必须在
子部分或
集合中有
Return
语句,并且您正在尝试返回值。你不能那样做

Sub Something
    Return 1 ' Error
End Sub
如果需要返回值,则需要函数:

Function Something As Integer
    Return 1 ' Ok
End Function

不能在Sub中返回某些内容,但可以在函数中返回

请看这里:

如何理解子项或集合

 'Return' statement in a Sub or a Set cannot return a value.
Sub
是一种没有返回值的方法:

Sub DoSomething()
    …
End Sub
(与函数
不同,函数是一种具有返回值的方法。)

Set
是属性的设置者:

Property X() As String
    Get
        Return SomeValue
    End Get
    Set(Value As String)
        SomeValue = Value
    End Set
End Property

与属性getter和函数不同,
Sub
s和
Set
ters不返回值,因此不能包含
return X
语句(它们可以包含裸
return
语句,该语句提前退出方法而不返回值,相当于
Exit Sub
Exit property
).

在Sub中,您可以有返回语句,但不能有“带值”,即:

但是

在属性集合中,您可以没有任何返回语句。 (另一方面,在属性中,必须有一个返回语句,其值符合属性的类型

Property MyProperty() As Integer
  Get
    ' do all kinds of stuff
    Return 3 ' Returns as integer-type value
  End Get
  Set(value as Integer)
    ' do stuff
    Return ' WRONG
    Return 3 ' also WRONG
  End Set
End Property
Set
可以包含
Return
语句,它相当于
Exit属性
Property MyProperty() As Integer
  Get
    ' do all kinds of stuff
    Return 3 ' Returns as integer-type value
  End Get
  Set(value as Integer)
    ' do stuff
    Return ' WRONG
    Return 3 ' also WRONG
  End Set
End Property