473,387 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Return value of a function

296 100+
I get the below warning :

Expand|Select|Wrap|Line Numbers
  1. Warning    1    Function 'exportToCSV' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.    C:\Documents and Settings\cxvngh\My Documents\Visual Studio 2008\Projects\OutCre\OutCre\Module1.vb    193    5    OutCre
To solve it I put a return statement "Return 1" in the code.

Is it the right way ?
Apr 14 '09 #1
2 1410
PRR
750 Expert 512MB
Your function does not return on all code paths
Expand|Select|Wrap|Line Numbers
  1. public static int Call()
  2.         {
  3.             int i = 90;
  4.  
  5.             if (i > 90)
  6.             {
  7.                 return i;
  8.             }
  9. // Here we need a default return... 
  10.         }
  11.  
  12.  
  13. public static int Call()
  14.         {
  15.             int i = 90;
  16.  
  17.             if (i > 90)
  18.             {
  19.                 return i;
  20.             }
  21.  
  22.             else
  23.             {
  24.                 return -1;
  25.             }
  26.         }
  27.  
Apr 14 '09 #2
Frinavale
9,735 Expert Mod 8TB
Just in case you don't understand what DeepBlue has said I'll restate it.

If you are creating a method that does not return anything then you should define it as a Sub.

For example, the following method definition is of a method that does not return a value:
Expand|Select|Wrap|Line Numbers
  1. Private Sub ExportToCSV()
  2.   'Your code goes here
  3. End Sub
  4.  
If your method returns something (say the path to the file you created) then you would define it as a Function.

For example, the following method returns a String:
Expand|Select|Wrap|Line Numbers
  1. Private Function ExportToCSV() As String
  2.   Dim path As String = "C:\MyFolder\MyCVSFileName.cvs"
  3.  
  4.   'Your code goes here
  5.  
  6.    return path
  7. End Function

You are getting that message because your Function is not returning a value.
If you have an "if" statement in your function that returns a value, you must also have a "default" return value for the function. In other words your function must always return something if it's a function.

For example, the following will cause an error because there is no default return value:
Expand|Select|Wrap|Line Numbers
  1. Private Function ExportToCSV() As String
  2.   Dim path As String = "C:\MyFolder\MyCVSFileName.cvs"
  3.   If someBoolean = True Then
  4.     return path
  5.   End If
  6.  
  7.   'Your code goes here
  8.  
  9. End Function
It is advisable to always have 1 return statement in a method just to keep things less confusing and to avoid this type of error.


-Frinny
Apr 14 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: strout | last post by:
function F(e) { return function(){P(e)} } Can anybody tell me what the code is doing? If return another function all in a function I would do function F(e)
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
16
by: G Patel | last post by:
Hi, If I want to call functions that don't return int without declaring them, will there be any harm? I only want to assign the function(return value) to the type that it returns, so I don't...
3
by: tshad | last post by:
I am trying to set up a class to handle my database accesses. I can't seem to figure out how to get the return value from my dataReader from these routines (most of which I got elsewhere). They...
5
by: Dmitriy Lapshin [C# / .NET MVP] | last post by:
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...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
18
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I...
20
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this...
2
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function ...
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.