Is it error-prone, yes. This is one of those things I see value in a Code
Critic/Code Analyzer/Lint type products that ensures that all functions
have
Jay, this reminds me of I bug I encountered in my own code because of this
behaviour.
To digresss a little, here is an excerpt of the MSHelp documentation.
If you use Exit Function [ed. or exit a function without returning a value]
without assigning a value to name, the function returns the default value
appropriate to argtype. This is 0 for Byte, Char, Decimal, Double, Integer,
Long, Short, and Single; Nothing for Object, String, and all arrays; False
for Boolean; and #1/1/0001 12:00 AM# for Date.
After reading this I naively thought I would end up with two dialog boxes
saying "01/01/0001 ..", 0, and throw an exception when attempting to display
the third dialog (casting Nothing.ToString() throwing an Exception). But no!
Public Class Form1
Inherits System.Windows.Forms.Form
'= Windows Form Designer generated code
Private Function test1() As Date
End Function
Private Function test2() As Integer
End Function
Private Function test3() As AcceptRejectRule
End Function
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("test1=" + test1.ToString())
MsgBox("test2=" + test2.ToString())
MsgBox("test3=" + test3.ToString())
End Sub
End Class
We can explain the behaviour but never-the-less, a trap for young players.
Hexathioorthooxalate
Jay B. Harlow said:
Dmitriy,
And I wonder why VB .NET keeps
silence and makes such function return some default value instead. Isn't it
error-prone?
Because VB.NET has defined a 'hidden' variable that is returned by the
function, this variable is the same name as the function. If you do not use
the Return statement, this variable is returned automatically. If you do not
set the variable the default value is used. So the check cannot simply check
for no Return statement, it would need to check that all code paths have set
this hidden variable or used the Return statement.
Is it error-prone, yes. This is one of those things I see value in a Code
Critic/Code Analyzer/Lint type products that ensures that all functions have
a return value, among other things. Rather then an outright compile warning.
Hope this helps
Jay
Dmitriy Lapshin said:
Hi all,
I think the VB .NET compiler should at least issue a warning when a function
does not return value. C# and C++ compilers treat this situation as an error
and I believe this is the right thing to do. And I wonder why VB .NET keeps
silence and makes such function return some default value instead. Isn't it
error-prone?
--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE