当函数没有返回时,VB.NET 中没有警告 [英] No warning in VB.NET when function has no return
问题描述
一些评论堆栈溢出问题 为什么 C# 编译器不停止引用自身的属性? 关于警告让我想到了在我编写更多 VB.NET 代码时总是让我感到困惑的老问题.
Some comments on Stack Overflow question Why doesn't the C# compiler stop properties from referring to themselves? regarding warnings got me thinking about old issues that always goofed me up when I was writing more VB.NET code.
其中之一是编译器不会警告您是否声明了一个函数,但从未执行过显式的 Return 语句或分配给函数名称.运行 Visual Studio 2008,我刚刚做了一个小的实验项目,似乎这个行为从来没有被修复过.我创建了一个 Visual Basic 控制台应用程序,代码如下:
One of them was the fact that the compiler didn't warn if you declared a Function but never did an explicit Return statement or assign to the Function name. Running Visual Studio 2008, I just made a small experimental project, and it seems as though the behavior has never been fixed. I created a Visual Basic Console application, with the following code:
Module MainModule
Sub Main()
Dim test As Boolean = TestWarning()
End Sub
Function TestWarning() As Boolean
Console.WriteLine("There is no Return Statement")
End Function
End Module
我还进入了项目设置并打开了On Option Strict
和Option Explicit
.我还设置了警告配置,以便没有返回值的函数/运算符".被设置为错误.
I also went into the Project Settings and turned On Option Strict
and Option Explicit
. I also set the Warning Configurations so that "Function/Operator with no return value" was set to Error.
我编译了项目,没有收到警告,TestWarning() 函数也没有错误.这似乎是放置警告的好地方,因为它默认为 False,而您可能只是忘记了返回.C# 将在没有 return 语句的情况下出错.我认为 VB.NET 对没有返回值的函数/运算符"做了同样的事情.配置.这是一个错误,还是我遗漏了什么?
I compiled the project and got no warning, and no error on the TestWarning() Function. This seems like a great place to put a warning, because it will default to False, and you may have simply forgotten to do a return. C# will error without a return statement. I thought that VB.NET did the same thing with the "Function/Operator with no return value" configuration. Is this a bug, or is there something I'm missing?
Function TestWarning() As Boolean
If DateTime.Now.DayOfWeek = DayOfWeek.Monday Then
Return False
Else
Console.WriteLine("There is no Return Statement")
End If
End Function
如果我在 If
中有一个显式的 Return
,而 Else
中没有任何内容,那么也不会出现警告或错误.它只会采用默认值,即使您可能打算(通过编程风格)获得显式返回.在这种情况下,我显式返回了 False
(这是 Boolean 的默认值),所以它可能是一个隐藏的错误,我应该在 Else<中返回
True
/代码>.
If I have an explicit Return
in an If
, and nothing in the Else
, there is also no warning or error. It will simply take the default, even though you likely intended (via programming style) to have an explicit return. In this case, I explicitly returned False
(which is the default for Boolean), so it's likely a hidden bug that I should have returned True
in the Else
.
推荐答案
该警告只会在函数默认返回 Nothing
时通知您.
The warning will only let you know when a function is going to return Nothing
by default.
如果返回值是引用类型,您会收到警告.
You would get a warning if return value was of a reference type.
但是你的函数有一个值类型的返回值,而那些不能是Nothing
.因此,没有警告.
But your function has a return value of a value type, and those cannot be Nothing
. Therefore, no warning.
这是因为这个函数内部的函数名充当结果变量.您可以通过将值分配给函数名称而不是使用 Return
来返回值.并且所有变量都使用默认值进行初始化,包括函数名变量.C
中不是这种情况,因此警告的含义不同.
This is because function name inside this very function acts as a result variable. You can return a value by assigning it to the function name instead of using Return
. And all variables are initialized with default values, including the function-name variable. This is not the case in C
, hence the different meaning of the warning.
将其与在初始化变量之前使用变量进行比较:
Compare this to using variables before initializing them:
Dim x As Integer
CallFunction(x) 'No warning, x is implicitly and properly initialized to 0.
Dim y as Object
CallFunction(y) 'A warning: variable used before a value is assigned to it
这篇关于当函数没有返回时,VB.NET 中没有警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!