How can i fix this error : Unreachable code detected in c#
You can't do anything after return
, so the trans.Rollback()
is unreachable:
change this...
catch
{
return false;
trans.Rollback();
}
to this:
catch
{
trans.Rollback();
return false;
}
In MSDN words:
The
return
statement terminates execution of the method in which it
appears and returns control to the calling method.
How to fix unreachable code detected error?
Instead of having an infinite loop, you could add a condition variable so that you can control when the while loop should break. It is only when your loop breaks that the code below the loop gets reached.
Below is an example of how you could add a condition variable to your code and how you can utilize this to control the state of your server.
private bool IsRunning = false;
public void StartServer()
{
int port = Convert.ToInt32(txtport.Text);
string ipaddre = getip();
IPAddress iP = IPAddress.Parse(ipaddre);
TcpListener serverSocket = new TcpListener(iP, port);
TcpClient clientSocket = default(TcpClient);
int counter = 0;
serverSocket.Start();
AppendTxtdata("Server Started , Waiting for Client Connection");
counter = 0;
IsRunning = true;
while (IsRunning)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
AppendTxtdata(" >> " + "Client No:" + Convert.ToString(counter) + " Connected" + clientSocket.Client.RemoteEndPoint.ToString()+"");
AppendTxtdata(Environment.NewLine);
handleClient client = new connectSuccess.handleClient();
client.startClient(clientSocket, Convert.ToString(counter));
}
**AppendTxtdata("Data sent to Client No :" + Convert.ToString(counter));**
clientSocket.Close();
serverSocket.Stop();
AppendTxtdata(" >> " + "exit");
}
public void StopServer()
{
IsRunning = false;
}
Why am I getting Unreachable code detected error
The unreachable code is the count++
statement in the for
loop. Since you return count;
on each pass through the for
loop (including, of course, the first pass), then the third part of the for
loop's statement will never be executed.
The Not all code paths return a value error is because you have this structure:
if (condition) {
for (blah-blah) {
//code
return count;
}
} else {
return 0;
}
If the conditions for the if
statement are met, but those for entering the for
loop are not met, then then program execution will skip the for
loop and the program won't hit a return statement.
Please: When you report an error, report where the error occurs. Those 4 numbers in parentheses after the file name (and before the error statement) tell you exactly where the error is. In this case, for example, the numbers point to the line number and the start and end of the code in error. My copy/paste of your code gave me this:
C:\Develop-Toss\TestConsole\TestConsole\Program.cs(137,45,137,50): warning CS0162: Unreachable code detected
The error started at line 137, character position 45 (the c
in count++
and ended at line 137, character position 50 (the first +
in count++
)
Your faulty logic (if (p0 < p) { for (int count = 0; p0 > p; count++) ...
) (i.e., that your code will never enter the for
loop) isn't the error. Once you fix the compilation errors, you'll need to fix the logic.
React JS Unreachable code no-unreachable
The unreachable aspect part of your code comes from the fact that once you return
, then the line below it won't execute. In this case:
var subs = this.state.menus[index_main].subs.map((item_sub, index_sub) => {
return(<h4 key={item_sub.sub_name}>{item_sub.sub_name}</h4>)
})
will never run with the return. This is what is causing your no-unreachable
linter error to fire.
From your post it's a bit ambiguous about what your desired behavior is, but my best guess is that you want an <h1>
for each item, followed by an <h4>
for each of your submenu items. In that case your code should look something like this:
render() {
return (
<div>
{
this.state.menus.map((item_main, index_main) => {
var subs = this.state.menus[index_main].subs.map((item_sub, index_sub) => {
return(<h4 key={item_sub.sub_name}>{item_sub.sub_name}</h4>)
});
return (
<div>
<h1 key={item_main.main_name}>{item_main.main_name}</h1>
{subs}
</div>
);
})
}
</div>
);
You can also use a <Fragment>
as the outer wrapper, but I didn't include that here for simplicity's sake.
Vue.js project compiling errors
Well your friend didn't use Eslint or he just ignored it.
You can ignore it too.
Go to your root directory of your app.
There should be an vue.config.js
file, if there isnt any file make one.
To deactivate eslint write in:
module.exports = {
lintOnSave: false
}
now stop your app and start it again with npm run serve
you should not see any eslint errors now.
These aren't "real" errors, its just , how to say, "beauty" errors, he did not follow the best practice how to write clean code.
.NET C# Unreachable code detected
See you have two code paths in your snippets, one is through if and the other is through else, you have return
statements in both paths, so the code after else block will not be executed and that is the error message telling. you have few options to get this solved.
Best option:
public string Insert_Data(string NewCol1 , string NewCol2, string NewCol3, string NewCol4, double NewCol5)
{
try
{
// your code here
if (Count > 0)
{
return "Record Already Exists.";
}
else
{
// code here
return "Inserted Sucessfully";
}
}
catch
{
return "Error occured";
}
finally
{
con.Close();
}
}
If you don't want to go with try..catch means you can use like this:
public string Insert_Data(string NewCol1 , string NewCol2, string NewCol3, string NewCol4, double NewCol5)
{
string resultMessage;
// your code here
if (Count > 0)
{
resultMessage = "Record Already Exists.";
}
else
{
// code here
resultMessage = "Inserted Sucessfully";
}
con.Close();
return resultMessage;
}
Related Topics
What's Wrong with This Division
The Difference Between Virtual, Override, New and Sealed Override
How to Click on the Radio Button Through the Element Id Attribute Using Selenium and C#
How to Update a Cell Value in a Db Table, Using SQL Server Ce and C# (Visual Studio 2010)
The Bare Minimum Needed to Write a Msmq Sample Application
String Interpolation VS String.Format
How to Login/Authenticate a User with ASP.NET MVC5 Rtm Bits Using Aspnet.Identity
What Value Should I Pass into Timezoneinfo.Findsystemtimezonebyid(String)
Total Number of Items Defined in an Enum
Calling Async Method on Button Click
Get the Object Is Null Using JSON in Wcf Service
What Does Linq Return When the Results Are Empty
How to Create Custom Http Status Codes
Best Way to Check for Nullable Bool in a Condition Expression (If ...)
Why Would One Ever Use the "In" Parameter Modifier in C#