Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
Question: My else-statement is unreachable, what am i doing wrong?
very VERY new at programming and i'm trying to compare the type so for example people can't enter strings when i'm asking for integers.
My code is probably pretty bad, if i could get a header what to do and why the if-argument skips the else-part i'd be really happy!
Thanks!
class Program
{
static void Main(string[] args)
{
int integer = 0;
start:
Console.WriteLine("How old are you?: ");
int svar = int.Parse(Console.ReadLine());
Utility.CompareTypes(svar, integer);
if (true)
{
Console.WriteLine("Thanks");
}
else
{
Console.WriteLine("You have to enter a number!");
goto start;
}
}
}
class Utility
{
public static bool CompareTypes<T01, T02>(T01 type01, T02 type02)
{
return typeof(T01).Equals (typeof(T02));
}
}
:c
It's not really a question of code, but of logic...
if (true) // <--- this will ALWAYS be true
{
Console.WriteLine("Thanks");
}
else // <--- therefore this will NEVER happen
{
Console.WriteLine("You have to enter a number!");
goto start;
}
Since your else block can never possibly execute under any logical circumstance, the entire block of code can be simplified to:
Console.WriteLine("Thanks");
In order for the else block to execute, the condition checked in the if statement needs to be false. You're currently not checking any actual condition, just a hard-coded true value.
Perhaps you meant to use the result of the previous line of code? Something like this:
var typesAreSame = Utility.CompareTypes(svar, integer);
if (typesAreSame)
{
//...
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I've come across some code where the variables are initialized to minus one. That's in some old code, is there any reason behind that? Because as far as I know all value types are initialized to zero.
I've tested the code and it doesn't change anything to leave the int variable uninitialized or with minus one, the result is the same.
Would you enlighten me?
class Program
{
static void Main()
{
SampleDelegate del = new SampleDelegate(SampleMethodOne);
del += SampleMethodTwo;
int Number = -1; //or -> int Number;
int returnedValue = del(out Number);
Console.WriteLine("returnedValue = {0}", returnedValue);
Console.ReadLine();
}
public static int SampleMethodOne(out int Number)
{
return Number = 1;
}
public static int SampleMethodTwo(out int Number)
{
return Number = 3;
}
}
public delegate int SampleDelegate(out int Number);
/returns 2
TL;DR: it depends, maybe there is no answer
Possible answer:
Initializing variable is better. you never know how it can be used in some later functions where having an unexpected value may be dangerous (when the code is optimized, you cannot be sure of the default value if not initialized).
In some case, an int may be used for some compatibility reason in the API when you just need an uint. In such a case, initializing to a negative value may be an easy way to detect an unset/invalid value.
No real reason, just an habit from the developer. I agree with comments, ask him if possible
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
i just started out with C# and im confused by a Compile Error.
This is the Code:
namespace Control_Flow_1
{
class Program
{
static void Main(string[] args)
{
string UserInput;
int Number;
Console.WriteLine("Bitte geben sie eine Nummer von 1-10 ein");
UserInput = Console.ReadLine();
Number = Convert.ToInt32(UserInput);
if (Number >= 1 && Number <= 10);
{
Console.WriteLine("Valid");
}
else
{
Console.WriteLine("Invalid");
}
}
}
}
The } after the Console.WriteLine; seems to do the Compile Error for some Reason, how to fix it ?
Compile Error:
if (Number >= 1 && Number <= 10); get rid of the semi colon.
With the semi colon after the if you have
if ()
a block of code
else (with no corresponding if)
a block of code
The semi colon ends the if statement and so you have a "floating" else with no previous if
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am trying to use this code for NET.reflector using Reflexil. I am trying to replace code with this:
if(Input.GetKeyDown(KeyCode.Keypad5)) {
int i = 0;
Character localPlayer = PlayerClient.GetLocalPlayer().controllable.GetComponent<Character>();
foreach (UnityEngine.Object obj2 in UnityEngine.Object.FindObjectsOfType(typeof(LootableObject)))
{
if (obj2 != null)
{
i++;
LootableObject loot = (LootableObject) obj2;
Debug.Log("Loot "+i+": "+loot.transform.position.ToString());
CCMotor ccmotor = localPlayer.ccmotor;
if(ccmotor != null && tpPos1 != Vector3.zero) {
ccmotor.Teleport(loot.transform.position);
Notice.Popup("", "Teleported to "+loot.name, 1.5f);
}
break;
}
}
}
But it gives me an error when I try to compile:
Line: 1 Column: 1 Error Number: CS0116 Error Message: "A namespace does not directly contain members such as fields or methods"
This is Unity code I think. I am not that experienced. Could anyone fix this for me? Or tell me what to do? Thanks!
The snippet you're showing doesn't seem to be directly responsible for the error.
This is how you can CAUSE the error:
namespace MyNameSpace
{
int i; <-- THIS NEEDS TO BE INSIDE THE CLASS
class MyClass
{
...
}
}
If you don't immediately see what is "outside" the class, this may be due to misplaced or extra closing bracket(s) }.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I need to print num after num(num-1) until zero
have to do it in recursion style.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("what is num?");
int num = int.Parse(Console.ReadLine());
downnums(num);
}
public static void downnums(int num)
{
if (num == 0)
Console.WriteLine("that all");
else
{
Console.WriteLine(downnums(num-1));
}
}
}
Thanks guys
Remember: void means your method doesn't return a value, so it cannot be assigned to other variable or passed to method as an argument like WriteLine method:
this is what you're looking for:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("what is num?");
int num = int.Parse(Console.ReadLine());
downnums(num);
}
public static void downnums(int num)
{
if (num == 0)
Console.WriteLine("that all");
else
{
Console.WriteLine(num);
downnums(num-1);
}
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm a student sitting a course in computing for the very first time and I've been trying to 'crack' this one for ages (I'm very new to this... sorry!). I can't seem to build a simple if/else statement in which if the conditions are met, it writes a positive response, and if not, a negative response. But I'm getting the errors:
Invalid expression term 'else'
Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Sorry if this makes no sense, I'm very confused! :(
static void Main(string[] args)
{
Console.WriteLine("Do you like bananas?");
Console.ReadLine();
if (Console.ReadLine() == "Yes") ;
{
Console.WriteLine("Thanks for your time!");
}
else;
{
Console.WriteLine("Oh okay.");
}
}
}
}
you need to remove the semicolon that you have after the else.
generally its
if (condition)
{
//code
}
else
{
//code
}
and if you have more than one condition to meet you can have
if (condition)
{
//code
}
else if (condition 2)
{
//code
}
else
{
//code
}
the last else is used if none of the conditions are met above, its like a default behavior
Further, you don't need braces for only one line of code
if (condition) DoInterestingStuff();
else DoNotDoInterestingStuff();
A few example conditions(with unnessesary if-statements for clarification):
if (bananas == true)
if (bananas) // same as above
if (bananas != false) // not equal to false, thus true
if (totalBananas >= 10)
No semi-colons:
if (Console.ReadLine() == "Yes")
{
Console.WriteLine("Thanks for your time!");
}
else
{
Console.WriteLine("Oh okay.");
}
Conditional Logic though is simple, basic, it is incredibly important to learn correctly. Your forgetting quite a bit of items:
Incorrect placement of ;.
You did:
if(Console.ReadLine() == "yes");
else;
The ; at the end of that particular portion of the statement is invalid. If your using Visual Studio or another IDE don't fight the tools, they try to help you with issues such as this. For instance a squiggly line should appear under the ; in that statement.
Your if else should be formatted in this manner:
if(value == "Yes")
{
Console.WriteLine("Thank you...");
}
else if (value == "No")
{
Console.WriteLine("No!");
}
else
{
Console.WriteLine("Ah...");
}
You'll want to familiarize yourself with the if else and switch statement early on. They're key piece in the foundation. You can find some more detail on the Microsoft Developer Network. Really good examples an explination of the entire process.