not all code paths return a value

G

Guest

I have a Winform news kiosk app which connects to a SQL database. When the
main form is running, it reaches a threshold time or number of sequential
operations and then loads another instance with a different dataset. The
datasets are based on local settings which I'm determining from IP space (all
machines running in that space will show the same news). The "special" set is
constructed at runtime so all important messages play in their entirty.

I keep getting the subject error on build. All functions work on their own,
but not as part of the form class. I don't see what I've done that will mess
it up. Here's the function to get the critical data. BTW, the ShortIPName
returns the first 3 segments of a machine's IP address.
string GetCommandText(string s)
{
try
{
string myIP = ShortIPName();
SqlConnection conn = new SqlConnection("Data Source=testSQL; Integrated
Security=SSPI;Initial Catalog=InfoScreens");
string scalarquery = "SELECT CmdCritical FROM Prefs WHERE IP = '" + myIP +
"'";
SqlCommand myCommand = new SqlCommand(scalarquery,conn);
myCommand.Connection.Open();
string rCommand = (string) myCommand.ExecuteScalar();
myCommand.Connection.Close();
return rCommand;
}

Thanks in advance,

E.
 
J

James Curran

string GetCommandText(string s)
{
try
{
string myIP = ShortIPName();
SqlConnection conn = new SqlConnection("Data Source=testSQL; Integrated
Security=SSPI;Initial Catalog=InfoScreens");
string scalarquery = "SELECT CmdCritical FROM Prefs WHERE IP = '" + myIP +
"'";
SqlCommand myCommand = new SqlCommand(scalarquery,conn);
myCommand.Connection.Open();
string rCommand = (string) myCommand.ExecuteScalar();
myCommand.Connection.Close();
return rCommand;
}

That's only half the function. At the very least we need a catch or
a finally and a closing brace to even get that to compile. What you've
shown as is fine. The problem is in the part you left our......

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
T

Tom Jones

Hey,

The error you are getting is not because of the code you posted. The error
is that you have an if statment somewhere and you are using a "return"
keyword in it.
 
J

Jon Skeet [C# MVP]

Tom Jones said:
The error you are getting is not because of the code you posted. The error
is that you have an if statment somewhere and you are using a "return"
keyword in it.

Actually, it could well be because of the code he's posted. The return
statement is in a try block. Presumably there's also a catch block, and
that doesn't have a return statement. There's no need for an "if"
statement to get that error message.
 
T

Tom Jones

Yea I saw that after I replied...

Jon Skeet said:
Actually, it could well be because of the code he's posted. The return
statement is in a try block. Presumably there's also a catch block, and
that doesn't have a return statement. There's no need for an "if"
statement to get that error message.
 
G

Guest

Thanks, James. Here's the catch block, but it's not doing anything more
specific or catching a sql-specific error.

The catch is nothing:
catch (Exception e)
{
MessageBox.Show(e.ToString());
}

The function ends after this block.

I've tried placing the return statement at the end of the try block and in a
finally block. I have also tried implicit initialization of the variables
locally and globally. I wonder if there is a connection pooling problem. The
form has an sqlDataAdapter that was created by the designer. I needed to
bypass it for this piece of code so a topical select statement can be
constructed (rCommand).

I can sledgehammer it to work, but that doesn't solve the problem.

E.
 
J

Jon Skeet [C# MVP]

Esteban404 said:
Thanks, James. Here's the catch block, but it's not doing anything more
specific or catching a sql-specific error.

Exactly. In other words, if an exception is thrown, nothing is being
returned. What do you expect to happen at that point? What do you want
it to return?

The problem has nothing to do with connection pooling - it has to do
with logical flow through your code.
 
G

Guest

Thanks for the reply, Jon.

This works just fine on it own, but it *never* fires in context. That's why
I thought some other problem is happening. I can throw specific errors, but
they none are more informative and I don't know what's happening to fix it
when it occurs. I'm still prototyping the application for best solution.

The function is just supposed to return the string retrieved from the scalar
query and plug it into the form's sqlCommand to build a dataset.

I made a copy of the solution so I can remove the designer code and put in
runtime initialized data to see if that works.
 
J

Jon Skeet [C# MVP]

Esteban404 said:
Thanks for the reply, Jon.

This works just fine on it own, but it *never* fires in context. That's why
I thought some other problem is happening. I can throw specific errors, but
they none are more informative and I don't know what's happening to fix it
when it occurs. I'm still prototyping the application for best solution.

In that case I'd just let the exception bubble up rather than catch it.
The function is just supposed to return the string retrieved from the scalar
query and plug it into the form's sqlCommand to build a dataset.

And what is it supposed to return if an exception occurs?
I made a copy of the solution so I can remove the designer code and put in
runtime initialized data to see if that works.

It won't if you've still got the try/catch in place.
 
G

Guest

I moved the variable init to the top of the function before the try block and
added a finally {} to finish up. Before it gets to the function, I test to
make sure it will fire or return "" string and test for that at the form
init(). Seems to work now. Testing time.

Thanks for the discussion.

E.

Tom Jones said:
Yea I saw that after I replied...
 
R

Ravichandran J.V.

You cannot create a method that returns a string only if the try is
successful because the compiler expects the return statement for this
function in the catch or the finally block too.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
G

Guest

That's very interesting. I'll look into that documentation. I do not see that
before. I have it working now, but I will try to mark up the previous code
and see if it fires with returns in the other blocks.

Thanks again, J.V.!
 
F

Frans Bouma [C# MVP]

Ravichandran said:
You cannot create a method that returns a string only if the try is
successful because the compiler expects the return statement for this
function in the catch or the finally block too.

this compiles fine:
public string Bla()
{
string toReturn = "Foo";
try
{
Console.WriteLine("Some output");
return toReturn;
}
catch(Exception ex)
{
throw ex;
}
finally
{
}
}

Frans.

--
 
R

Ravichandran J.V.

Without looking at anything else just the

not all code paths return a value

that the OP stated is suffiecient because this compiler message appears
not just in .Net but in any languages.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
J

Jon Skeet [C# MVP]

John Wood said:
That's because the throw is a return statement also.

It's not - certainly not by the language specification definition.

(For one thing, it doesn't even guarantee that control leaves the
method.)
 
R

Reginaldo Rigo

This error happens when write the return command inside a loop where the
compiler suspects the code will not be evaluated all the time.

example:

private string GetToken(string TokenString, int TokenPos)
{
int x, i, j;
x = 1; i = 0; j = 0;
while ( i < TokenPos )
{
j = x;
while (( x <= TokenString.Length ) && (
TokenString.Substring(x,1).Trim() != ";" ))
{
x++;
}
i++;
x++;
//// try put return here
}
return TokenString.Substring(j, x - j - 1).Trim();
}


The return command inside the loop cause the error.


Reginaldo
 

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