How do you get around this?

W

Water Cooler v2

Sorry for the stupid question, but I've been there and done that. Just
that just now I have forgotten it for the moment. It happens when
you're in a totally different rut of thought. Bail me out here.

Just to quote an example:

static void Main(string[] args)
{
Console.WriteLine((new Class1()).SomeMethod((Object)19));
}

public int SomeMethod(System.Object obj)
{
if (obj == null)
throw new System.Exception("The argument is null.");

if ((int)obj >= 10)
return (int)obj;
}


Error: Class1.cs(13): 'Exception.Class1.SomeMethod(object)': not all
code paths return a value



How do you get out of this?
 
M

Marc Gravell

Just tell it what you want to do if obj is <= 9... return something else?
throw an ArgumentException? dance a Tango? (the latter not supported by
1.1... or 2.0...)

Also - maybe it is from the simplification from a more complex code sample,
but I'm not sure why obj needs to be an object in this case, since we rule
out null and then cast to int, this doesn't leave many options... how about

public int SomeMethod(int value) {
return value >= 10 ? value : 0; // returns 0 if value isn't >= 10; could
throw an ex instead
}
 
K

Kevin Spencer

Hi Walter,

public int SomeMethod(System.Object obj)
{
//...
if ((int)obj >= 10)
return (int)obj;
}

What does it return if (int)obj is NOT greater than 10?

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Sequence, Selection, Iteration.
 
W

Water Cooler v2

Yup! Did that just after making the post.

The problem under question actually was:


public System.Data.IDataReader GetReader(string SQL)
{
System.Data.IDataReader ret = null;

if (this._connection.UnderlyingConnection == null)
{
System.Exception ex = new DatabaseTypeNotSupportedException();
_log.Write(new LogEntry(ex), true);
throw ex;
}

if (this._connection.Database == Databases.MSAccess)
ret = (new OleDbCommand(SQL,
(OleDbConnection)this._connection.UnderlyingConnection)).ExecuteReader();
else if (this._connection.Database == Databases.SQLServer)
ret = (new SqlCommand(SQL,
(SqlConnection)this._connection.UnderlyingConnection)).ExecuteReader();

return ret;
}
 
P

Pritcham

Is it because you're not returning a value if the parameter passed is
null? (I know you're throwing an exception but the warning points to
the fact that this particular code path doesn't return anything.

HTH
 
C

Carl Daniel [VC++ MVP]

Water said:
Sorry for the stupid question, but I've been there and done that. Just
that just now I have forgotten it for the moment. It happens when
you're in a totally different rut of thought. Bail me out here.

Just to quote an example:

static void Main(string[] args)
{
Console.WriteLine((new Class1()).SomeMethod((Object)19));
}

public int SomeMethod(System.Object obj)
{
if (obj == null)
throw new System.Exception("The argument is null.");

if ((int)obj >= 10)
return (int)obj;

What if obj is < 10? You still need to return something.
}


Error: Class1.cs(13): 'Exception.Class1.SomeMethod(object)': not all
code paths return a value



How do you get out of this?

Return something from all paths, just like the error says.

-cd
 
S

Steve Barnett

The question should be... what does SomeMethod return if "obj < 10". You
have defined the function as returning an int, so it must return an int. As
coded, if "obj>=10" then you return a value. If this test fails, you do not
return anything.

Steve
 
M

Marc Gravell

You just need a final else; what should it do if it isn't Access or
SqlServer? Perhaps throw the DatabaseTypeNotSupportedException used earlier?
Because if somebody throws Oracle at your method, it will not have a value
for "ret". And it doesn't care if all enum values are exausted etc... it
must have a legal exit value or an exception...

Marc
 
J

Jon Skeet [C# MVP]

Pritcham said:
Is it because you're not returning a value if the parameter passed is
null? (I know you're throwing an exception but the warning points to
the fact that this particular code path doesn't return anything.

No, it's not the null code path that's causing the problem - it's the
non-null, less than 10 code path. The compiler is perfectly happy for
something to guarantee it will throw an exception instead of returning
a value.

Jon
 
G

Guest

Water Cooler v2 said:
Sorry for the stupid question, but I've been there and done that. Just
that just now I have forgotten it for the moment. It happens when
you're in a totally different rut of thought. Bail me out here.

Just make sure all paths return a value. In your example:

public int SomeMethod(System.Object obj)
{
int i = 0;
if (obj == null)
throw new System.Exception("The argument is null.");
if ((int)obj >= 10)
i = (int)obj;
return i;
}

--
Timm Martin
Mini-Tools
..NET Components and Windows Software
http://www.mini-tools.com

Just to quote an example:

static void Main(string[] args)
{
Console.WriteLine((new Class1()).SomeMethod((Object)19));
}

public int SomeMethod(System.Object obj)
{
if (obj == null)
throw new System.Exception("The argument is null.");

if ((int)obj >= 10)
return (int)obj;
}


Error: Class1.cs(13): 'Exception.Class1.SomeMethod(object)': not all
code paths return a value



How do you get out of this?
 
C

Claes Bergefall

No, it doesn't need a final else. If it isn't Access or SqlServer the code
returns null since that is what ret is initialized to. This is perfectly
valid as far as the compiler is concerned

/claes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top