C# ureachable code detected with if and do while [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int x = 0;
do
{
if (false)
{
Console.WriteLine("hold UP");
}
++x;
Console.WriteLine(x);
} while (false);
}
}
}

That's the compiler warning you that some part of your code will never be executed. In your case it refers to this part.
if (false)
{
Console.WriteLine("hold UP");
}
A similar thing would happen in cases like this:
void Test()
{
return;
// more code here.
}
//or
void Test2()
{
while(true) //loop forever.
{ }
// more code here.
}

Console.WriteLine("hold UP"); is unreachable because if (false) will never be true, thus the code in the following block can never be executed.

Andy is correct. For the code to run inside an If statement the conditions must be true. What you have there is a static condition of False, thus it will not enter into the If statement.

Related

Can’t get a specific output using a boolean [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want a specific output depending on wether the Boolean is set to true or not. I can’t seem to find the solution so I’m just asking here. I don’t what to output „true“ or „false“ but a specific text.
What I tried:
public class Program
{
bool isAlive = true;
public static void Main()
{
if (isAlive == true){
Console.WriteLine("Is True");
}
}
}
What worked:
public class Program
{
public static void Main()
{
bool isAlive = true;
if (isAlive == true){
Console.WriteLine("Is True");
}
}
}
The Error was that I tried to use the non-static variable in the static void. Thanks to everyone that helped.
you can do either
string output = bool_variable ? "true value" : "false value";
Debug.Log(output);
or
if(bool_variable == true){
Debug.Log("true value")
}else{
Debug.Log("false value")
}
Edit:
You are getting the non-static error because you are trying to access isAlive from a static method when it is not static. So you either need to create an instance of Program, make isAlive static, or remove the static keyword from Main.
Further more this does not seem to be a unity question since you are using Console.WriteLine and not implementing MonoBehaviour
You can use the following code
{boolean_variable} ? "Boolean value is true" : "Boolean value is false"

Whats wrong with my recursive factorial example code in C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
So I'm currently learning C# and I'm on Recursive Factorials, I followed instructions to create an example of the base case and the recursive case. But the program doesn't seem to be identifying my integer.
using System;
namespace Recursive_factorial
{
class Program
{
static void Main()
{
Factorial(5);
}
public static int Factorial(int n)
{
if (n == 0)
{
return 1; //base case
}
else
{
return n * Factorial(n - 1); //recursive case
}
}
}
}
maybe the problem is that you never show the result, try with
using System;
namespace Recursive_factorial
{
class Program
{
static void Main()
{
Console.WriteLine(Factorial(5));
}
public static int Factorial(int n)
{
if (n == 0)
return 1;
else
return n * Factorial(n - 1);
}
}
}
Also you can add
Console.ReadKey();
Below the Console.WriteLine, to pause the execution before it closes

The Code Won't Execute all [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have the following code:
private void G1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
decimal quantity = 0, cost = 0;
decimal totalstock = 0, newtotalstock = 0;
if (decimal.TryParse(G1.Rows[e.RowIndex].Cells["RealExport"].Value.ToString(), out quantity) && decimal.TryParse(G1.Rows[e.RowIndex].Cells["Cost"].Value.ToString(), out cost))
{
decimal price = quantity * cost;
G1.Rows[e.RowIndex].Cells["Total"].Value = price.ToString();
}
if (decimal.TryParse(G1.Rows[e.RowIndex].Cells["TotalStock"].Value.ToString(), out totalstock) && decimal.TryParse(G1.Rows[e.RowIndex].Cells["RealExport"].Value.ToString(), out quantity))
{
newtotalstock = totalstock - quantity;
G1.Rows[e.RowIndex].Cells["TotalStock"].Value = newtotalstock.ToString();
return;
}
decimal avai = 0, newavai = 0;
if (decimal.TryParse(G1.Rows[e.RowIndex].Cells["RealExport"].Value.ToString(), out quantity) && decimal.TryParse(G1.Rows[e.RowIndex].Cells["AvailableStock"].Value.ToString(), out avai))
{
newavai = avai - quantity;
G1.Rows[e.RowIndex].Cells["AvailableStock"].Value = newavai.ToString();
return;
} }
The problem is, it only execute 2 out of 3 the code, I mean, when the newtotalstock is calculated, the code will end.
I try to change the newavai to up above, and the result is the same, it will calculate the newavai and pass the newtotalstock. I dont know why, all the code are correct. Please help
word "return" ends function, if you are using void type method you don't really need to use return, unless you want to quit it at certain point. The code after "return" will never be executed (only exception might be during usage "yield return", but that is another story).
void someMethod()
{
doSomething();
return;
StartWorldWarThree(); //no waries, this will never be executed :)
}
Furethermore you can always make a breakpoint in your code (click on the left border of your window, or just read about it) and then check how your code is being executed :) F10/F11 to make step in your code, F5 to go to next breakpoint or end execution, if there are no more breakpoints.
Taking in count that all conditions are true the return; will stop executing the rest of the code, if you want the 3 if to be executed, remove the return; and place it the last line of the method as
void Method() {
if() {}
if() {}
if() {}
return;
}
Or dont place it at all, because that method is void and does not need it

C# how to break public Void [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have an class like this (this is an example ) I need return whole of class from everywhere I need it
Line1 :Public void test(bool start)
Line2 :{
Line3 : if (start) //do something;
Line4 : else // i use else for update with help other code and... forget it
Line5 : {
Line6 : if (xxX)
Line7 : if (sadasdas)
Line8 : if(sdaeqwr324f)
Line9 : if (Asdasdsa)
Line10: for (int a=0;a<6;a++)
Line11: {
Line12: if (c[a]==1321334)
Line13: {
Line14: t=c[1];
Line15: /* <<< i neeeed reset trace of test to first line of test Line16 :>>>*/
Line17: }
Line18: }
Line19: there are about 400 lines codes like Up at under of this line }
Line20:}
My target is line 15 – if I add a return at The tracer jump to line 17 – I need tracer jump to line 2
Sorry for my bad English ( I search to find why to solve it but I cant find anythings )
To "break" from a function use return:
public void test(bool start)
{
if (start) //do something;
else // i use else for update with help other code and... forget it
{
//do somethings (it is loop )
pleas reset trace of test to first line of test
return; // This ends the method, and returns to the caller
}
}

How to know that a void runs? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
want to ask about void type, so I could know that it works or no
in PHP i could have a condition like this:
if(mysql_query($query))
{ bla bla }
else
{ print error }
how to do like that on ASP.NET?
i'm trying like this:
if (k.EditPassword(username.Text, oldPassTxt.Text, newPassTxt.Text) == true )
{
Response.Redirect("sample.aspx");
}
else
{ print error }
but of course, it cannot be like that, because void isn't boolean
Usually void functions that do work that can fail will have some other way of informing you that they failed. Often they will throw an Exception:
try
{
k.EditPassword(...)
}
catch(ApplicationException ex)
{
// print Exception
}
Response.Redirect(...)
Other times they will set a status variable or something:
k.EditPassword(...)
if (k.Result == Result.OK)
Response.Redirect(...)
else
// print error...
Looking at documentation or source code for the conditions you are trying to handle is the only way to know how to handle it.
You can use a literal control and add your text to it. So your code will go something like,
if (k.EditPassword(username.Text, oldPassTxt.Text, newPassTxt.Text) == true )
{
Response.Redirect("sample.aspx");
}
else
{ Literal1.Text = error; }
Literal should be in design file, you can add it from toolbox.
But a better and proper way would be to,
Log it. (You would need a log mecahnism)
Write a unit test :)

Categories

Resources