472,983 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,983 software developers and data experts.

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

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
5 8008
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
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
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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: c | last post by:
RH9, Apache 2.0.49, php 4.3.6 been away from php for about 2 years and this way used to work, still does in older php versions, now it doesn't. is there a reason for this? a work around? Thanx...
6
by: kelvSYC | last post by:
This little bit of seeminly innocent code seems to give me these two errors, all on the line that declares check(). Is there some part of C++ that I'm missing out on? class Condition { public:...
13
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled...
11
by: ajikoe | last post by:
Hello, I used Visual C# Standard Edition. I want to comment my program using xml commentary method, I don't know why if I use value and example tag, it is not working / showed in the html...
10
by: mdh | last post by:
Quick question about pointers. Wrote this "trying to understand this better" code: int *ptr, x = 565; ptr= &x; printf("\n\n\nThe value of x is %d\n", x);
13
by: alvin.yk | last post by:
Hi, Normally, a piece of code such as <a href="http://www.yahoo.com" onclick="alert('hello');return false;">link</a> will stop the browser from actually going to href's destination....
21
by: Steven T. Hatton | last post by:
I'm trying to improve my formal understanding of C++. One significant part of that effort involves clarifying my understanding of the vocabulary used to describe the language. This is from the...
8
by: mdh | last post by:
May I ask this. Given the declaration: int myf( int, int); and a function pointer: (*fp)=int myf(int, int); where I am initializing fp to point at myf....or trying to..
3
by: moltendorf | last post by:
I copied the files from my "test" database on my old server (MySQL was not running) to my new server ("./mysql/data/test" folder), and after starting the server, SHOW TABLES; shows all of the tables...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.