By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,569 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,569 IT Pros & Developers. It's quick & easy.

Why there's no "Function does not return value" warning?

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Have you switched on option strict?

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:%2***************@TK2MSFTNGP11.phx.gbl...
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

Nov 20 '05 #2

P: n/a
Actually, forget that, I just tried it and even though strict is on, I still
don't get a compile time error that nothing was returned.

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:%2***************@TK2MSFTNGP11.phx.gbl...
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

Nov 20 '05 #3

P: n/a
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 [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:%2***************@TK2MSFTNGP11.phx.gbl... 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

Nov 20 '05 #4

P: n/a
In article <#H*************@TK2MSFTNGP11.phx.gbl>, Dmitriy Lapshin [C# / .NET MVP] wrote:
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?


Yes it is error prone, and I agree with you. I don't know if this is an
attempt to stay compatible with VB.CLASSIC code - but I wish there was
at least an option to change this behavior.

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #5

P: n/a
> 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 [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:u7**************@TK2MSFTNGP10.phx.gbl... 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 [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:%2***************@TK2MSFTNGP11.phx.gbl...
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



Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.