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 5 years ago.
Improve this question
I'm new to programming and I have just installed Visual Studio 2017. I created this code (from the book I'm learning), but this does not compile. I have problem with string interpolation and I get error:
Unexpected character '$',
but I'm using C# 6.0 so this should not be a problem ?
static void Main(string[] args)
{
string comparison;
WriteLine("Enter the number:");
double var1 = ToDouble(ReadLine());
WriteLine("Enter another number :");
double var2 = ToDouble(ReadLine());
if (var1 < var2)
comparison = "less than";
else
{
if (var1 == var2)
comparison = "equal to";
else
comparison = "greater than";
}
WriteLine($ "The first number is {comparison} the second number");
ReadKey();
}
It is a very small problem :) Remove space after $:
WriteLine($"The first number is {comparison} the second number");
See proper structure under documentation:
$"<text> {<interpolated-expression> [,<field-width>] [:<format-string>] } <text> ..."
I've requested an edit that explains that there must be no spacing after the $ and now it states:
Related
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 2 years ago.
Improve this question
when I try to use TryParse I get error CS0029, pls see below:
static void Main(string[] args)
{
double m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12;
double notval = 0;
Console.WriteLine("Please insert the rainfall in January:");
m1 = double.TryParse(Console.ReadLine(), out notval) ;
I get the error "Cannot implicitly convert type 'bool' to 'double'" (CS0029).
TryParse returns of it was successful at parsing or not, and puts the result (if successful) in the out parameter (on you case notval).. so use:
if(double.TryParse(Console.ReadLine(), out notval))
{
// Do what you want with notval
}
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 2 years ago.
Improve this question
int age;
string name;
Console.WriteLine ("Hello!");
Console.WriteLine ("What is your name?");
name = Console.ReadLine();
if (name = Max H); // **error is here**
{
Console.WriteLine ("Psst you're a nerd lmao");
}
else{
Console.WriteLine ("Hello, " + name );
}
Basically I want it so if there's a specific answer it'll give a different reply to the default answer.
main.cs(13,16): error CS0103: The name 'Max H' does not exist in the
current context =
The error I get.
TwoThree errors:
Comparing needs a double =, so ==
That "Max H" is a string constant, so must be in double quotes.
After the if, you don't want a ; as that ends the if. The next line will be executed always, except that the compiler will trip over that else (thanks RetiredNinja)
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 2 years ago.
Improve this question
(beginner here, I'm learning c#) I've just learned about equality operators and was testing one out. For some reason unbeknownst to me (it's probably some really simple mistake I'm overlooking) I'm getting an error. Here's the code:
string number = "number";
number == "number";
I'm getting an error for the line, number == "number". To my knowledge, when I run it, "true" should be printed. Thanks for helping out a beginner, I'll probably be kicking myself once I know the answer.
In the second line you use the equality operator ==. You correctly understand that the equality operator == returns true if its operands are equal, false otherwise. Thus, it returns a value of type bool. But to output the result of this operation to the console, you should use the method Console.WriteLine. So you should first save this value in a variable and then output the value of this variable to the console. This can be done like this:
string number = "number";
bool equalityComparisonResult = number == "number";
Console.WriteLine(equalityComparisonResult);
Or you can do without an intermediate variable and print the result of the equality directly to the console:
string number = "number";
Console.WriteLine(number == "number");
string number = "number";
number == "number"? Console.WriteLine("numbers are equal"): Console.WriteLine("numbers are different");
or you can use Equals Method:
string number = "number";
number.Equals("number")
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 5 years ago.
Improve this question
I am asking this question because I have minimal understanding of the c sharp language and the Object-oriented paradigm. The following program was created, based on a few youtube videos and programming articles, from stack-overflow and various other sources.
case "-":
textBox.Text = (valor - Double.Parse(textBox.Text).ToString());
break;
case "*":
textBox.Text = (valor * Double.Parse(textBox.Text).ToString());
break;
case "/":
textBox.Text = (valor / Double.Parse(textBox.Text).ToString());
The following code segment is giving me the following errors
Operator '-' cannot be applied to operands of type 'double' and 'string'
for the 3 cases
Thank you
Any sort of help and advice is more than welcome
You got misplaced .ToString(). move all of .ToString()s after ).
Like this:
textBox.Text = (valor - Double.Parse(textBox.Text)).ToString();
You are parsing the decimal value out of your textbox
Double.Parse(textBox.Text)
Then calling ToString() afterwards (effectively un-parsing your decimal). You are getting an error because you are trying to subtract a string:
Double.Parse(textBox.Text).ToString()
From your valor decimal
Double.Parse(textBox.Text) returns a double but then you are calling ToString() which returns a string.
So you have textBox.Text = <double> - <string>
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
For some reason, the VSE C# 2010 (or maybe the problem is in my laptop) is not interpreting the <, >, <= and >= Boolean comparison operators correctly.
static void Main(string[] args)
{
Console.WriteLine("Enter an integer:");
int myInt = Convert.ToInt32(Console.Read());
bool isLessThan10 = myInt < 10;
bool isBetween0and5 = (0 <= myInt) && (myInt <= 5);
Console.WriteLine("Integer less than 10? {0}", isLessThan10);
Console.WriteLine("Integer between 0 and 5? {0}", isBetween0and5);
Console.WriteLine("Exactly one of the above is true? {0}",
isLessThan10 ^ isBetween0and5);
Console.ReadKey();
}
Enter an integer:
2
Integer less than 10? False
Integer between 0 and 5? False
Exactly one of the above is true? False
Console.Read reads in a single character and returns the ASCII value of that character. The ASCII value of the character 2 is not 2.
You want to read in the character and get it's representation as a character, which you can do through the use of Console.ReadKey (as you do later in your program) or Console.ReadLine if you want to read a string of characters interpreted as such, rather than using Console.Read. You can then convert that string representation of a number into it's numeric representation using int.Parse.