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")
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
I am using IndexOf() and it does not work.
The code is:
if (sqlex.Message.IndexOf("Critical") > 0)
and Message does contain the word - Critical (see image), but does not equate to true. It takes the false path.
I also put in test code:
int index = sqlex.Message.IndexOf("Critical");
and index is = 0.
Why?
The first position in the string is 0, not 1. So if your string is
Critical Error - do not continue. Contact IT...
And you search for Critical, then 0 is the expected result.
If the string is not found, the result will be -1 ... unless the string is also empty, so make sure to check that, too.
The method string.IndexOf returns -1 if the character or string is not found. 0 means it is found. Change your code to if (sqlex.Message.IndexOf("Critical") >= 0)
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
I am enrolled in a beginner C# online course.
Learning about how to deal with coding errors is mostly for the student to resolve.
Visual Studio 2019 is the preferred course IDE.
Some VS error messages are easy while others are very tricky to understand and resolve.
I am working on an assessable item - so not posting the full program code here.
A key section of code is throwing error CS1501 - I have searched internet for solution though do not understand how those maters fit with my situation.
Key code section as follows:
int abc;
abc = Console.ReadLine(Convert.ToInt32());
Curly red lines in VS are under 'ToInt32' with error notes: int
Convert.ToInt32(bool value) (18+ overloads) CS1501 No overload
for method 'ToInt32' takes 0 arguments.
Thanks.
You have it the wrong way around
int abc;
abc = Convert.ToInt32(Console.ReadLine());
The long story
Console.ReadLine() returns a string
Convert.ToInt32 takes an input (of varying type) to return an int
If you look at all the overloads for Convert.ToInt32 you will soon notice there are no variants that take 0 arguments. Which is what the compiler is telling you.
No overload for method 'ToInt32' takes 0 arguments.
However, never use Convert.ToInt32 to parse user input
Use int.TryParse instead
Converts the string representation of a number to its 32-bit signed
integer equivalent. A return value indicates whether the operation
succeeded.
int abc;
var input = Console.ReadLine();
if(!int.TryParse(input, out abc))
// user couldn't type a number, do something else instead
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
you can see that value is 74 but its not going inside the if why?
Because you have a space in TEMP. " 74". The two values are not equal because the second one TEMP2 is "74" without the space.
Try using Trim on your values to remove whitespace:
string temp = ids[i].Trim();
string temp2 = raditem.Value.ToString().Trim();
Also, always use the .Equals overload for the String class, as you can control the culture for the string comparison. In this case it doesn't matter because they're numerals but it could matter if you want to compare to alphabetical strings where you want "A" to equal "a" and so forth.
Furthermore, if you are sure your array contains only numbers, I could recommend converting the values to numbers before comparing them.
// one way to validate is wrap this in a try-catch and handle input format error.
int temp = Convert.ToInt32(ids[i].Trim());
That way if you have a case where there aren't numbers, you could validate and complain to the user or whatever you want.
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 having issues trying to set a bunch of Numeric form controls from a dictionary of values. I want to be able to check if the corresponding key exists in the dictionary before attempting to set the Numeric control's value.
My problem is that the ternary operator evaluates both resulting expressions even if only one of them is going to be used. This throws a "Key not found" exception from the Dictionary.
Is there an easy way to set a numeric form control to the value in a dictionary only if the corresponding key exists (and if not 0)?
(Position is an enum and MyCoordinate is a struct containing a list of axes)
Dictionary<Position, MyCoordinate> pos = DataManager.Instance.ToolChanger.GetPositionCoordinates();
pos1TNumeric.Value = pos.ContainsKey(Position.ElectrodePosition01) ? 0M : (decimal)pos[Position.ElectrodePosition01].Axes[EDMAxis.T];
pos2TNumeric.Value = pos.ContainsKey(Position.ElectrodePosition02) ? 0M : (decimal)pos[Position.ElectrodePosition02].Axes[EDMAxis.T];
pos3TNumeric.Value = pos.ContainsKey(Position.ElectrodePosition03) ? 0M : (decimal)pos[Position.ElectrodePosition03].Axes[EDMAxis.T];
pos4TNumeric.Value = pos.ContainsKey(Position.ElectrodePosition04) ? 0M : (decimal)pos[Position.ElectrodePosition04].Axes[EDMAxis.T];
pos5TNumeric.Value = pos.ContainsKey(Position.ElectrodePosition05) ? 0M : (decimal)pos[Position.ElectrodePosition05].Axes[EDMAxis.T];
My current solution is probably going to just make a wrapper function that is passed the dictionary key and returns the correct decimal value. I'm wondering if there's an easier way. Thanks!
I think you have to switch your statements:
pos1TNumeric.Value = pos.ContainsKey(Position.ElectrodePosition01) ? (decimal)pos[Position.ElectrodePosition01].Axes[EDMAxis.T] : 0M;
So if the key exists, you take the value. Otherwise take your default value.
var value = bool_expression ? if_true : if_false;
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
How to check if a label is visible?This is not working...
if(labelFS.Visible = false)
{do sth}
else//labelFS.Visible = true
{do other}
solved
Use == to compare two values, not the assignment operator (a single =).
By using a single =, you're actually setting the value of labelFS.Visible to False, which hides it.
(There's also no need to type out true and false .)
if (labelFS.Visible)
// do something
else
// do something else
I'd suggest reading up on the difference. Here's a start...
Equality Operator (==)
Assignment Operator (=)
if(labelFS.Visible == false)
{do sth}
else//labelFS.Visible == true
{do other}
Use == instead of =